How to modify a Collection while iterating using for-each loop without ConcurrentModificationException? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-26 08:56:46

问题


This question already has an answer here:

  • Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop 24 answers

If I modify a Collection while iterating over it using for-each loop, it gives ConcurrentModificationException. Is there any workaround?


回答1:


Use Iterator#remove.

This is the only safe way to modify a collection during iteration. For more information, see The Collection Interface tutorial.

If you also need the ability to add elements while iterating, use a ListIterator.




回答2:


One work around is to save your changes and add/remove them after the loop.

For example:

List<Item> toRemove = new LinkedList<Item>();

for(Item it:items){
    if(remove){
        toRemove.add(it);
    }
}
items.removeAll(toRemove);



回答3:


A second workaround is to use a collection class whose iterators won't give the exception. For example ConcurrentLinkedQueue, ConcurrentHashMap and so on.

These avoid the need to throw exceptions by providing weaker models of consistency for the iterators. (Of course, you need to understand those models, and decide whether they are suitable for your application.)

They are typically a bit slower than non-concurrent collections, but faster than the synchronized collection wrappers if there is significant contention.




回答4:


If you just want to remove the element from the collection, you can use Iterator instead of Iterable.

Otherwise, what you can do is not to iterate the original collection, but first make a copy of the list. For example if your collection is a list, than you can make a new ArrayList(originaList) and iterate over that. The modification should be done to the original list.

Another alternative which maybe better for your use case, is not to use for-each but the traditional for-.



来源:https://stackoverflow.com/questions/6958478/how-to-modify-a-collection-while-iterating-using-for-each-loop-without-concurren

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!