[SOLVED] How to create ConcurrentHashSet before and after Java 8

[SOLVED] How to create ConcurrentHashSet before and after Java 8

There are at least two ways to create ConcurrentHashSet in Java. One of them works in Java 8 or newer. The second one works well in older versions of Java.

Problem
You want to create  ConcurrentHashSet but it is missing in the java.util.concurrent package. As an explanation – the package contains some concurrent implementations of collection and map interfaces.

 

Cause
The implementation of  ConcurrentHashSet is missing because you can always get it from  ConcurrentHashMap or just use a special implementation of  SortedSet called ConcurrentSkipListSet.

 

Solution A: ConcurrentHashSet derived from ConcurrentHashMap (Java 8 or newer)
If you use at leat Java 8 in your project, this is recommended solution for the problem of missing  ConcurrentHashSet class.

 

Solution B: ConcurrentHashSet derived from ConcurrentHashMap
If you use an older Java version you can retrieve ConcurrentHashSet from dummy ConcurrentHashMap instance.

 

Solution C: Use ConcurrentSkipListSet
If you need a sorted set implementaion or you don’t care about momory overhead, you can always use ConcurrentSkipListSet implementation from the java.util.concurrent package.

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu