[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to handle ConcurrentModificationException

Posted on 2009-07-15
3
Medium Priority
?
321 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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

650 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