Solved

avoiding java.util.ConcurrentModificationException in for loops

Posted on 2006-06-13
3
2,765 Views
Last Modified: 2007-12-19
What is the best way to avoid java.util.ConcurrentModificationException in the for loop below:

import java.util.Collection;
import java.util.Iterator;
import java.util.TreeSet;

import junit.framework.TestCase;

public class Test extends TestCase {


 
   public void testLoop()
   {
      Collection currentCollection = new TreeSet();
      currentCollection.add("AAA");
      currentCollection.add("BBB");
      currentCollection.add("CCC");
     
      Collection updatedCollection = new TreeSet();
      updatedCollection.add("CCC");
      updatedCollection.add("DDD");
      updatedCollection.add("EEE");
     
      for (Iterator iter = currentCollection.iterator(); iter.hasNext();) {
         String element = (String) iter.next();
         if(!updatedCollection.contains(element))
         {
            currentCollection.remove(element);
         }
         
      }
      assertTrue(currentCollection.contains("DDD"));
      assertTrue(currentCollection.contains("EEE"));
      assertFalse(currentCollection.contains("CCC"));
     
   }
   
}
0
Comment
Question by:aturetsky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 16897118
Try changing

>>currentCollection.remove(element);

to

iter.remove();
0
 
LVL 1

Author Comment

by:aturetsky
ID: 16898009
great, thanks !
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16898060
:-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question