Java concurrent modification exception

后端 未结 4 909
南笙
南笙 2021-01-21 08:24

I have written following code which is resulting in concurrent modification exception. How can I prevent it ? The idea is to escape all values of the Map and reconstruct the obj

4条回答
  •  礼貌的吻别
    2021-01-21 09:11

    ConcurrentModificationException is thrown when you use fail-fast iterators (eg: entries.iterator() is fail-fast iterator). What they do is they iterate over original collection object. To be able to modify and iterate over a collection object you can use fail-safe iterator (eg: List books = new CopyOnWriteArrayList<>()) This will take a copy inside memory while iterating over the elements and even you will be able to modify it.

提交回复
热议问题