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
You remove it using iterator.remove().
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).
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.