【发布时间】:2014-05-14 16:55:19
【问题描述】:
我最近做了一个例子,我在迭代时将元素添加到ConcurrentHashMap。
代码sn-p -
Map<String, String> map = new ConcurrentHashMap<String, String>();
map.put("ABC", "abc");
map.put("XYZ", "xyz");
map.put("MNO", "mno");
map.put("DEF", "def");
map.put("PQR", "pqr");
Iterator<Map.Entry<String, String>> entrySet = map.entrySet().iterator();
while(entrySet.hasNext()) {
Map.Entry<String, String> entry = entrySet.next();
System.out.println(entry.getKey()+", "+entry.getValue());
map.put("TQR", "tqr");
}
但我无法找到在 CHM 的情况下代码不抛出 ConcurrentModificationException 的确切原因。
简而言之,是什么让 CHM 不像 HashMap 那样抛出 ConcurrentModificationException。
谢谢!
【问题讨论】:
-
“简而言之,是什么让 CHM 不像 HashMap 那样抛出 ConcurrentModificationException?”其实施;它被设计为同时使用。感兴趣的可以看看the implementation。
标签: java map hashmap hashcode concurrenthashmap