When we remove an object by using remove() method of a
Collection or List while an Iterator thread is traversing it, we get
ConcurrentModificationException. If an Iterator detects any
structural change in Collection it can throw
ConcurrentModificationException
xxxxxxxxxx
public class concurrentmodificationexception {
public static void main(String[] args) {
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1, 1);
map.put(2, 2);
map.put(3,3);
Iterator<Integer> it = map.keySet().iterator();
while(it.hasNext()) {
Integer key = it.next();
System.out.println("Map Value:" + map.get(key));
if (key.equals(2)) {
map.put(1, 4);
}
}
}
}