ConcurrentModificationException (Java)

隐身守侯 提交于 2019-11-29 07:55:54

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).

You remove it using iterator.remove().

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.

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