Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no ConcurrentHashSet against ConcurrentHashMap

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?

like image 991
Talha Ahmed Khan Avatar asked Aug 09 '11 07:08

Talha Ahmed Khan


People also ask

Can ConcurrentHashMap can throw ConcurrentModificationException?

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.

Do we have lock while getting value from ConcurrentHashMap?

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.

Is ConcurrentHashSet thread-safe?

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.

Can we replace Hashtable with ConcurrentHashMap?

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.


2 Answers

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.

like image 103
Ray Toal Avatar answered Sep 28 '22 23:09

Ray Toal


Set<String> mySet = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); 
like image 35
Serge Mask Avatar answered Sep 28 '22 22:09

Serge Mask