[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

avoiding java.util.ConcurrentModificationException in for loops

Posted on 2006-06-13
3
Medium Priority
?
2,790 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

649 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