Suppose you need inter-thread shared unordered Set
of elements. Options are CopyOnWriteArraySet
and ConcurrentHashMap
-backed Set
created by Collections.newSetFromMap(new ConcurrentHashMap<E, Boolean>())
. Both are thread-safe, both are safe to iterate and do not throw ConcurrentModificationException
, but unlike map-backed one, CopyOnWriteArraySet
has O(n)
complexity on mutation operations.
So the question is in which cases CopyOnWriteArraySet
is really better?
Thanks in advance!