How to Create a Thread-Safe ConcurrentHashSet in Java?
Last Updated :
20 Jul, 2022
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():
Java
ConcurrentHashMap<String, Integer> map
= new ConcurrentHashMap<>();
Set<String> set = map.newKeySet();
set.add( "geeksforgeeks" );
set.add( "geeks" );
set.contains( "geeks" );
set.remove( "geeksforgeeks" );
|
This above-mentioned example is not the only way for creating a thread-safe Set in Java.
ConcurrentHashSet using keySet(default value):
Java
ConcurrentHashMap<String, Integer> map
= new ConcurrentHashMap<>();
Set<String> set
= map.keySet( 246 );
set.add( "GeeksForGeeks" );
|
Java
ConcurrentHashMap<String, Integer> map
= new ConcurrentHashMap<>();
Set<String> set
= map.keySet( 246 );
set.add( "GeeksForGeeks" );
|
Implementation:
Java
import java.io.*;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
class GFG {
public static void main (String[] args) {
ConcurrentHashMap<String,Integer> map = new ConcurrentHashMap<>();
map.put( "Geeks" , 246 );
map.put( "GeeksforGeeks" , 246 );
map.put( "Java" , 200 );
map.put( "Java 8" , 200 );
map.put( "Threads" , 300 );
Set concSet = map.keySet();
System.out.println( "Initial set: " + concSet);
Set <String> penNameSet = ConcurrentHashMap.newKeySet();
penNameSet.add( "Flair" );
penNameSet.add( "Reynolds" );
penNameSet.add( "Cello" );
System.out.println( "before adding element into concurrent set: " + penNameSet);
penNameSet.add( "Classmate" );
System.out.println( "after adding element into concurrent set: " + penNameSet);
if (penNameSet.contains( "Reynolds" ))
{
System.out.println( "YES" );
}
else
{
System.out.println( "NO" );
}
System.out.println(penNameSet.contains( "Reynolds" ));
penNameSet.remove( "Cello" );
System.out.println( "after removing element from concurrent set: "
+ penNameSet);
}
}
|
Output
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 raptors are able to keep a snapshot of the array unchanged when building the iterator.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...