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?
ConcurrentHashMap does not throw ConcurrentModificationException if the underlying collection is modified during an iteration is in progress. Iterators may not reflect the exact state of the collection if it is being modified concurrently.
ConcurrentHashMap: It allows concurrent access to the map. Part of the map called Segment (internal data structure) is only getting locked while adding or updating the map. So ConcurrentHashMap allows concurrent threads to read the value without locking at all.
2 ConcurrentHashSet using keySet(default value)It is also thread-safe, so can be used in multi-threading Java applications. You can learn more about set-based operations on The Complete Java MasterClass - Updated for Java 11.
Is it safe to replace the Hashtable instances with ConcurrentHashmap instances for performance gain? In most cases it should be safe and yield better performance. The effort on changing depends on whether you used the Map interface or Hashtable directly.
There's no built in type for ConcurrentHashSet
because you can always derive a set from a map. Since there are many types of maps, you use a method to produce a set from a given map (or map class).
Prior to Java 8, you produce a concurrent hash set backed by a concurrent hash map, by using Collections.newSetFromMap(map)
In Java 8 (pointed out by @Matt), you can get a concurrent hash set view via ConcurrentHashMap.newKeySet()
. This is a bit simpler than the old newSetFromMap
which required you to pass in an empty map object. But it is specific to ConcurrentHashMap
.
Anyway, the Java designers could have created a new set interface every time a new map interface was created, but that pattern would be impossible to enforce when third parties create their own maps. It is better to have the static methods that derive new sets; that approach always works, even when you create your own map implementations.
Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With