Java ConcurrentHashSet Example

Java 8 finally allows us, the programmers, to create thread-safe ConcurrentHashSet in Java. Before then, it was simply not possible. There were variations that tried to improvise the implementation of the above-mentioned class, one of which would be, using ConcurrentHashMap with a dummy value. However, as you might have guessed, all of the improvisations of ConcurrentHashMap had their limitations and risks.

Java 8 allows us to use keySet(defaultVal) and newKeySet() methods that return the Set, which happens to be a proper set. This gives access to many necessary functions that the user can use: contains(), remove(), etc. Note: these methods are available in ConcurrentHashMap, not in ConcurrentMap interface, that means that a variable of type ConcurrentHashMap has to be created and will act as a reference. Another way would be a simple cast of the object.

There are many Collection classes that are included in the Java Concurrency API like CopyOnArrayList for ArrayList, ConcurrentHashMap for HashMap and CopyOnWriteArraySet for HashSet. However, despite all these examples, there is nothing like ConcurrentHashSet. Many developers say that they can use ConcurrentHashMap with same values to implement the set they want, however the problem with this approach is that instead of a set, you have a map. Which would therefore result in the inability to perform set operations on the ConcurrentHashMap with the dummy values. Simply put, it is not a Set.

There were other improvisations trying to create ConcurrentHashSet but all of them are now in the past as Java 8 has added newKeySet() which returns a Set which is backed by a ConcurrentHashMap.




How to create a ConcurrentHashSet in Java 8

ConcurrentHashMap<String, Integer> example = new ConcurrentHashMap<>(); 
Set<String> exampleSet = example.newKeySet(); 
exampleSet.add("example"); 
exampleSet.add("example2");
exampleSet.contains("example2"); 
exampleSet.remove("example"); 

In the example above we create the set, and also add 2 elements to it, checking if it contains a certain element and removing a certain element.

Note: this is the only way of creating thread-safe Set in Java.

Java program to create ConcurrentHashSet using the new methods added on java.util.concurrent.ConcurrentHashMap class.

import java.util.Set; 
import java.util.concurrent.ConcurrentHashMap; 

public class Example { 
   public static void main(String[] args) throws Exception { 
      ConcurrentHashMap shoesCost = new ConcurrentHashMap<>(); 
      shoesCost.put("Nike", 80); 
      shoesCost.put("Adidas", 40); 
      shoesCost.put("Reebok", 76); 

      Set shoeCostSet = shoesCost.keySet(); 
      
      shoeCostSet = shoesCost.newKeySet(); 
      System.out.println("before adding element into concurrent set: " + shoeCostSet);
      shoeCostSet.add("Puma"); 
      System.out.println("after adding element into concurrent set: " + shoeCostSet); 
      shoeCostSet.contains("Adidas"); 
      shoeCostSet.remove("Reebok"); 
  } 
} 

Output:

before adding an element into the concurrent set: [Nike, Adidas, Reebok] 
after adding an element into the concurrent set: [Nike, Adidas, Reebok, Puma] 

 

Leave a Reply

avatar