I want to have a concurrent multimap (a map from a key to a list of values) in Java, something like the following:
var map = new ConcurrentHashMap<String, List<String>>();
Is the following operation thread-safe, or is there a chance for a race-condition and losing one value in case of concurrent updates?
map.computeIfAbsent(key, k -> new CopyOnWriteArrayList<>()).add(value);
From what I understand, the first operation computeIfAbsent()
is atomic, so there cannot be two threads running this code and get different instances of the ArrayList, and the returned CopyOnWriteArrayList
instance is also thread safe, so add()
should be fine. Is my reasoning correct?
(let's say I cannot use any libraries, so please don't suggest Guava, etc).