What is the best way to create a copy of a Map in ...
# coroutines
s
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
If targeting JVM, then you can use a ConcurrentHashMap.
🙏 1
e
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
🙏 1
108 Views