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

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();
}

Leave a Comment