Exception in thread main java.util.ConcurrentModificationException
Answer: If you are getting below exception:
Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793) at java.util.HashMap$KeyIterator.next(HashMap.java:828) at com.javahonk.setTest.KeySetTest.main(KeySetTest.java:26)
It means you are trying to modify collection while iterating it. By design all collection classes in java.util.package are fail fast and we get exception.
To fix this issue you could do below:
- Don’t try to modify direct collection itself use iterator remove method if you are removing element form collection. Please see example below:
package com.javahonk.iteratorenumeration; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class AvoidCCMException { public static void main(String[] args) { List<String> arrayList = new ArrayList<String>(); arrayList.add("Java"); arrayList.add("Honk"); arrayList.add("CCMTest"); //avoid concurrent modification exception Iterator<String> iterator = arrayList.iterator(); while (iterator.hasNext()) { String string = (String) iterator.next(); System.out.println(string); if (string.equalsIgnoreCase("Honk")) { iterator.remove(); } } System.out.println("\nbelow code will throw exception"); //throw concurrent modification exception iterator = arrayList.iterator(); arrayList.remove(0); while (iterator.hasNext()) { String string = (String) iterator.next(); System.out.println(string); } } }
Output:
If your requirement is different where iterator method does not support it then go for java.util.concurrent package classes to do modification work. In below example will show you how to use ConcurrentHashMap and CopyOnWriteArrayList class form java.util.concurrent package.
Modify map using ConcurrentHashMap:
package com.javahonk.setTest; import java.util.Iterator; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapTest { public static void main(String[] args) { Map<String,String> map = new ConcurrentHashMap<String, String>(); map.put("Java", "Java"); map.put("Honk", "Honk"); map.put("Test", "Test"); Iterator<String> iterator = map.keySet().iterator(); while(iterator.hasNext()) { String key = (String)iterator.next(); String val = (String)map.get(key); map.put("New Value", "New Value"); System.out.println("Key:" + key + " Value: " + val); } } }
Example: Modify list using CopyOnWriteArrayList:
package com.javahonk.setTest; import java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; public class CopyOnWriteArrayListTest { public static void main(String[] args) { CopyOnWriteArrayList<String> coArrayList = new CopyOnWriteArrayList<String>(); coArrayList.add("Java"); coArrayList.add("Honk"); coArrayList.add("Test"); //remove element while iterating it System.out.println("Origional list: \n"); for (Iterator<String> iterator = coArrayList.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); coArrayList.remove("Test"); System.out.println(string); } //Print modifed list System.out.println("\nModified list: \n"); for (String string : coArrayList) { System.out.println(string); } } }