Solved

avoiding java.util.ConcurrentModificationException in for loops

Posted on 2006-06-13
3
2,738 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 70
Books that can get me started on JAVA 2 56
word0 challenge 4 53
micro services vs rest web services 16 53
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now