java.util.ConcurrentModificationException

April 15, 2009

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.

Share and Enjoy:
  • Twitter
  • Google Buzz
  • DZone
  • Digg
  • Reddit
  • Diigo
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • email
  • PDF
  • Add to favorites

tags: ,
posted in Java.util Package by shyam

Follow comments via the RSS Feed | Leave a comment | Trackback URL

1 Comment to "java.util.ConcurrentModificationException"

  1. tuna wrote:

    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]

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org