私はこのようなハッシュテーブルを持っています:
Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put("A", "one");
ht.put("B", "two");
次に、そのvalues()
メソッドを呼び出してその値を取得し、次のように Collection オブジェクトに格納します。
Collection<String> col = ht.values();
このコレクション オブジェクトには次の値があります。
one, two.
それから私は電話col.add(" three");
しました今回はこのエラーが発生しました:
Exception in thread "main" java.lang.UnsupportedOperationException.
APIを確認しました:
If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false)
remove()
コレクションに追加した値 ("three") は一意であり、重複していませんclear()
。
を呼び出すadd()
ことができません。追加できないのはなぜですか?