HashSet is based on HashMap.
If we look at HashSet<E>
implementation, everything is been managed under HashMap<E,Object>
.
<E>
is used as a key of HashMap
.
And we know that HashMap
is not thread safe. That is why we have ConcurrentHashMap
in Java.
Based on this, I am confused that why we don't have a ConcurrentHashSet which should be based on the ConcurrentHashMap
?
Is there anything else that I am missing? I need to use Set
in a multi-threaded environment.
Also, If I want to create my own ConcurrentHashSet
can I achieve it by just replacing the HashMap
to ConcurrentHashMap
and leaving the rest as is?
ConcurrentSkipListSet
is built onConcurrentSkipListMap
, which implementsConcurrentNavigableMap
andConcurrentMap
. – BainbridgeConcurrentSkipListSet
for sorting as well, the bottom storage is amap
, but unfortunately not aHash
one. I don't think this has an excuse to not support it, maybe comes in future edition. – Kerril