我有一个哈希表是这样的:如何增加价值到收藏
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)
,我加入(“三”),以收藏的价值是独一无二的,没有duplicate.But我可以做其他诸如remove()
和clear()
等操作。
无法拨打add()
。为什么不允许添加?