I recently did an example where I added the element to ConcurrentHashMap
while iterating over it.
Code snippet -
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");
}
But I'm unable to find the exact reason as to why the code doesn't throw a ConcurrentModificationException in case of CHM.
In short, what it is that makes CHM not to throw ConcurrentModificationException, unlike HashMap.
Thank you!