Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a ConcurrentModificationException thrown when removing an element from a java.util.List during list iteration? [duplicate]

@Test public void testListCur(){     List<String> li=new ArrayList<String>();     for(int i=0;i<10;i++){         li.add("str"+i);     }      for(String st:li){         if(st.equalsIgnoreCase("str3"))             li.remove("str3");     }     System.out.println(li); } 

When I run this code,I will throw a ConcurrentModificationException.

It looks as though when I remove the specified element from the list, the list does not know its size have been changed.

I'm wondering if this is a common problem with collections and removing elements?

like image 335
hguser Avatar asked Feb 25 '11 02:02

hguser


People also ask

How do I overcome Java Util ConcurrentModificationException?

How do you fix Java's ConcurrentModificationException? There are two basic approaches: Do not make any changes to a collection while an Iterator loops through it. If you can't stop the underlying collection from being modified during iteration, create a clone of the target data structure and iterate through the clone.

Why does iterator remove Do not throw ConcurrentModificationException?

Notice that iterator. remove() doesn't throw an exception by itself because it is able to update both the internal state of itself and the collection. Calling remove() on two iterators of the same instance collection would throw, however, because it would leave one of the iterators in an inconsistent state.

What causes Java Util ConcurrentModificationException?

What Causes ConcurrentModificationException. The ConcurrentModificationException generally occurs when working with Java Collections. The Collection classes in Java are very fail-fast and if they are attempted to be modified while a thread is iterating over it, a ConcurrentModificationException is thrown.


2 Answers

I believe this is the purpose behind the Iterator.remove() method, to be able to remove an element from the collection while iterating.

For example:

Iterator<String> iter = li.iterator(); while(iter.hasNext()){     if(iter.next().equalsIgnoreCase("str3"))         iter.remove(); } 
like image 151
Paul Blessing Avatar answered Sep 20 '22 04:09

Paul Blessing


The Java 8 way to remove it from the List without Iterator is:

li.removeIf(<predicate>) 

i.e.

List<String> li = new ArrayList<String>(); // ... li.removeIf(st -> !st.equalsIgnoreCase("str3")); 
like image 23
bigdev.de Avatar answered Sep 22 '22 04:09

bigdev.de