?
Solved

How to handle ConcurrentModificationException

Posted on 2009-07-15
3
Medium Priority
?
316 Views
Last Modified: 2013-11-23
As obvious, the attached code  is a recipe of ConcurrentModificationException. But how to handle when you need to make changes i.e. remove some items from list while iterating over.
List<String> strList = new ArrayList<String>();
		strList.add("A");
		strList.add("B");
		strList.add("C");
		strList.add("D");
		strList.add("E");
		strList.add("F");
		strList.add("G");
		strList.add("H");
		
		for (String str : strList) {
			if (str.equals("E"))
				strList.remove(str);
			System.err.println(str);
		}

Open in new window

0
Comment
Question by:fix2009
[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
3 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 900 total points
ID: 24863196
You have to use an Iterator, then use its remove() method. It's a slight drawback of the enhanced for loop feature.
0
 
LVL 13

Assisted Solution

by:Bart Cremers
Bart Cremers earned 500 total points
ID: 24863211
You should work with a real iterator
List<String> strList = new ArrayList<String>();
strList.add("A");
strList.add("B");
strList.add("C");
strList.add("D");
strList.add("E");
strList.add("F");
strList.add("G");
strList.add("H");
 
Iterator<String> it = strList.iterator();
 
while (it.hasNext()) {
    String str = it.next();            
    if (str.equals("E"))
        it.remove();
        System.err.println(str);
    }
}

Open in new window

0
 

Author Closing Comment

by:fix2009
ID: 31603929
Thanks Guys.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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