How to avoid java util ConcurrentModificationException when iterating through and removing elements from an ArrayList

0 votes

I wanted to iterate over an ArrayList and remove the elements, but I am getting an error: "java.util.ConcurrentModificationException".

My code:

public class Test() {
    private ArrayList<A> abc = new ArrayList<A>();

    public void doStuff() {
        for (A a : abc) 
        a.doSomething();
    }

    public void removeA(A a) {
        abc.remove(a);
    }
}

What is the best practice to handle this problem? Should I clone the list first?

May 23, 2022 in Java by Kichu
• 19,040 points
865 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

There are two options to solve this:

  • Add a new list of values you want to remove to the list within the loop, then call: originalList.removeAll(valuesToRemove).
  • Use the remove() method on the iterator.

An example of the second option:

List<String> list = new ArrayList<String>();

...

for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {

    String value = iterator.next();

    if (value.length() > 5) {

        iterator.remove();

    }

}

I hope this helps you.

answered May 23, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Java

0 votes
1 answer

Exception Handling: “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it

Iterator<String> iter = myArrayList.iterator(); while (iter.hasNext()) { ...READ MORE

answered Jan 10, 2019 in Java by Sushmita
• 6,920 points
825 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,889 views
0 votes
2 answers
0 votes
2 answers

Remove duplicate elements from an ArrayList in Java

List<String> al = new ArrayList<>(); // add elements ...READ MORE

answered Jul 26, 2018 in Java by Sushmita
• 6,920 points
1,917 views
0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,920 points
1,283 views
0 votes
1 answer

How to download and save a file from Internet using Java?

public void saveUrl(final String filename, final String ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,020 views
0 votes
2 answers

What is the difference between Type List and type ArrayList in Java

By List, you actually tell, that your object ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
2,779 views
0 votes
1 answer

Which is faster amongst an Array and an ArrayList?

Array is faster in to an access ...READ MORE

answered Feb 16, 2022 in Java by Saurabh Maurya

edited Mar 5 28,698 views
0 votes
0 answers
0 votes
0 answers

Benefits of arrays

The benefits of a list over an ...READ MORE

Aug 3, 2022 in Java by krishna
• 2,820 points
779 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP