How to avoid ConcurrentModificationException when iterating over a map and changing values?

前端 未结 7 1013
眼角桃花
眼角桃花 2020-12-06 09:46

I\'ve got a map containing some keys (Strings) and values (POJOs)

I want to iterate through this map and alter some of the data in the POJO.

The current code

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 10:19

    Try using ConcurrentHashMap.

    From JavaDoc,

    A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.

    For ConcurrentModificationException to occur, generally:

    it is not generally permissible for one thread to modify a Collection while another thread is iterating over it.

提交回复
热议问题