The below mentioned stand alone java applications namely TestConcurrentModificationException and EscapeConcurrentModificationException demonstrate the cases when java.util.ConcurrentModificationException exception is thrown and when not. And how to fix it.
import java.util.Iterator;
import java.util.LinkedHashMap;
/**
*
* @author shyam
*/
public class TestConcurrentModificationException {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
LinkedHashMap<string,String> processedMap=new LinkedHashMap<string,String>();
System.out.println("processedMap.size() Before population ->"+processedMap.size());
for(int i=0;i<1000;i++){
processedMap.put(i+"",i+"");
}
System.out.println("processedMap.size() After population ->"+processedMap.size());
//Incorrect use
Iterator<string> processedItr = processedMap.keySet().iterator();
while(processedItr.hasNext()){
String key=processedItr.next();
//below mentioned case is valid, which resets the value to an existing key.
processedMap.put(key,"any value");
//below mentioned cases cause java.util.ConcurrentModificationException
processedMap.remove(key);
processedMap.put("newkey","any value");
}
System.out.println("processedMap.size() After processedMap.remove() ->"+processedMap.size());
}
}
If u need to iterate through and modify a collection then use the following program which escapes from java.util.ConcurrentModificationException with a newly constructed replica of the original collection to iterate through and modifies(remove/put a new key value mapping) the original collection, when needed.
import java.util.Iterator;
import java.util.LinkedHashMap;
/**
*
* @author shyam
*/
public class EscapeConcurrentModificationException {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
LinkedHashMap<string,String> processedMap=new LinkedHashMap<string,String>();
System.out.println("processedMap.size() Before population ->"+processedMap.size());
for(int i=0;i<1000;i++){
processedMap.put(i+"",i+"");
}
System.out.println("processedMap.size() After population ->"+processedMap.size());
//correct use
LinkedHashMap<string,String> processedMapReplica=new LinkedHashMap<string,String>(processedMap);
Iterator<string> processedItr = processedMapReplica.keySet().iterator();
while(processedItr.hasNext()){
String key=processedItr.next();
//below mentioned cases cause java.util.ConcurrentModificationException
processedMap.remove(key);
processedMap.put("newkey","any value");
//below mentioned case is valid, which resets the value to an existing key.
processedMap.put(key,"any value");
}
System.out.println("processedMap.size() After processedMap.remove() ->"+processedMap.size());
}
}
Share knowledge and Save Nature
-Shyam.
















This is very good solution and it works,
It prevents java.util.ConcurrentModificationException
How did you come with this idea it is really creative solution ? = )
[Reply]
Link | October 21st, 2011 at 4:20 PM