前幾天使用
HashSet 時遇到了一個
java.lang.UnsupportedOperationException 異常,在網上看了看前輩們的總結,再加上自己對 JDK 中相關 API 的學習,將出現該異常的原因基本弄清楚了。現在用一個簡短的小實例演示這個異常的出現:
1 Map<String, String> m1 = new HashMap<String, String>();
2 Map<String, String> m2 = new HashMap<String, String>();
3
4 m1.put("foo", "foo");
5 m2.put("bar", "bar");
6
7 Set<String> s = m1.keySet();
8 s.addAll(m2.keySet());
9 //s.add("konglong");
10
11 /*Set<String> s = new HashSet<String>();
12 s.addAll(m1.keySet());
13 s.addAll(m2.keySet());*/
14
15 System.out.println(s);
注釋掉第 8 行,釋放第 9 行或者注釋掉第 9 行,釋放第 8 行,運行,就會發生 java.lang.UnsupportedOperationException 異常;但是如果注釋掉第8、9行代碼,釋放第11、12、13行代碼則不會出現該異常。原因主要在于引用 s 所指向的 Set 對象是 HashMap.keySet 方法的返回值,此方法在 JDK 文檔中的描述為:
public Set<K> keySet()
- Returns a
Set
view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are #ff0000. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.
從上述描述中得知此方法返回的是當前 HashMap 對象中所包含的 key 的視圖,這個視圖是以什么格式保存的呢?它是以一個 HashSet 對象的格式(形式)保存的,也就是將一個一個的鍵值封裝到一個 HashSet 對象中。但是并不意味著返回的這個 HashSet 對象就和原來的那個 HashMap 對象一刀兩斷,毫無干系了,它們仍然藕斷絲連,相互影響著。這個 HashSet 對象是由原來那個 HashMap 對象在背后默默無聞地支持著。所以你對原來那個 HashMap 對象所做的修改會映射到這個 HashSet 對象,反之,對這個 HashSet 對象的修改亦會映射到原來那個 HashMap 對象。如果在對這個 set 對象進行迭代的同時修改了映射(但是通過迭代器自己的 remove 操作除外),則迭代結果是不確定的。該 set 支持元素的移除,通過 Iterator.remove、Set.remove、removeAll、retainAll 和 clear 操作可從該映射中移除相應的映射關系。但是它不支持 add 或 addAll 操作。
我們可以淺嘗輒止,就此止步。但是很多時候我們稍稍堅持一下就可以得到更多的回報。它為什么不支持 add 或 addAll 操作呢?因為如果它支持這些操作,那么原來的 map 對象中必然多了幾個 key,但是你不能只 put key 值進去,而沒有對應的 value 值,這違背了映射本身的數據結構。
下一篇我將用實例演示一下通過 keySet() 方法返回的 Set 對象和它源自的 Map 對象之間的相互作用。