What is the best way to create a copy of a Map in Kotlin without causing ConcurrentModificationException in a multi-threaded environment?
I almost would like
map.toMap()
to happen atomically. I wanted to see whether something exists before creating my own synchronization blocks.
m
mkrussel
04/13/2022, 7:54 PM
If targeting JVM, then you can use a ConcurrentHashMap.
🙏 1
e
ephemient
04/13/2022, 10:10 PM
there isn't a way to atomically read an arbitrary map. but certain specific maps such as ConcurrentHashMap and CopyOnWriteMap (not standard, but exists in several libraries) support iteration during concurrent modification