Creating Thread Safe ConcurrentHashSet is not possible before JDK 8 because of the java.util.concurrent package does not have a class called ConcurrentHashSet, but starting with JDK 8, the newly added keySet (the default) and newKeySet() methods to create a ConcurrentHashSet in Java that is supported by ConcurrentHashMap.
ConcurrentHashSet can be created by using ConcurrentHashMap as it allows us to use keySet(default value) and newKeySet() methods to return the Set, which happens to be a proper Set. This gives us access to necessary functions like contains(), remove(), etc. These methods can only be used in the ConcurrentHashMap class and not in the ConcurrentMap interface, so we need to use the ConcurrentHashMap reference variable to hold the reference, or we can use a type conversion to convert the ConcurrentHashMap object stored in the ConcurrentMap variable.
The problem with this method is that there is only one map and no set, and it cannot perform a set operation on ConcurrentHashMap using virtual values. When some methods require a Set, you can’t pass it, so it’s not very useful.
Another option is by calling the keySet() method and the keySet() method actually returns a Set, in which the Set operation can be executed and passed but this method has its limitations that we cannot add new elements to this keyset because it will throw an UnsupportedOperationException.
Due to all these limitations newKeySet() method introduced which returns a Set supported by a given type of ConcurrentHashMap, where the value is Boolean.
How to create ConcurrentHashSet using newKeyset():
This above-mentioned example is not the only way for creating a thread-safe Set in Java.
ConcurrentHashSet using keySet(default value):
Initial set: [Threads, Java, GeeksforGeeks, Geeks, Java 8] before adding element into concurrent set: [Cello, Reynolds, Flair] after adding element into concurrent set: [Cello, Classmate, Reynolds, Flair] YES true after removing element from concurrent set: [Classmate, Reynolds, Flair]
These are the methods to create ConcurrentHashSet in Java 8. The JDK 8 API has all the major features like lambda expression and streams along with these small changes which make writing code easy.
The following are some important properties of CopyOnWriteArraySet:
- It is best suited for very small applications, the number of read-only operations far exceeds variable operations, and it is necessary to prevent interference between threads during traversal.
- Its thread is safe.
- Rasters do not support variable delete operations.
- The traversal through the iterator is fast and does not encounter interference from other threads.
- The rators are able to keep a snapshot of the array unchanged when building the iterator.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.