ConcurrentModificationException (Java)

后端 未结 3 1124
北恋
北恋 2020-12-18 08:05
Exception in thread \"main\" java.util.ConcurrentModificationException
Squash the PC dirties the room Violet. The room\'s state is now dirty
Lily the animal growls
T         


        
相关标签:
3条回答
  • 2020-12-18 08:46

    You remove it using iterator.remove().

    0 讨论(0)
  • 2020-12-18 08:55

    The only safe way to remove an element from an underlying collection and continue the iteration is to use the remove() method of the Iterator. This removes the last element returned by the next() method of the Iterator.

    In your case, it appears that this would require passing the Iterator to the method that performs the modification (or make it an instance field, like the Map object is already).

    0 讨论(0)
  • 2020-12-18 08:55

    Another option is to use ConcurrentHashMap which doesn't have this issue. You can use this as a drop in replacement and you don't need to change the rest of the code.

    0 讨论(0)
提交回复
热议问题