How to modify a Collection while iterating using for-each loop without ConcurrentModificationException? [duplicate]
问题 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