There is no ConcurrentHashSet inwards JDK 8 but yous tin withal practise 1 for yourself past times using the ConcurrentHashMap class of java.util.concurrent package. There is a novel method added into ConcurrentHashMap inwards JDK 8, newKeySet(), which allows yous to practise a concurrent hash ready backed past times a concurrent hash map. If yous scream upward whenever yous larn keys from the map they are returned inwards a Set e.g. for the former keySet() method because map entirely has unique keys. Since map doesn't let duplicate keys, it tin last used every bit a set, every bit long every bit yous entirely attention for keys or simply 1 element. That's why Java designers are added newKeySet() method to convert a map to set. This is besides a static method, which agency yous don't quest a ConcurrentHashMap object to practise a concurrent hash set.
The Set returned past times the keySet() method is backed past times a ConcurrentHashMap amongst given keys in addition to values are ever Boolean.TRUE. It is besides overloaded to convey initial capacity to avoid internal re-sizing. If yous know, the HashSet class besides internally uses an HashMap where values are ever same, an object, which is possible because Map allows duplicate values.
Unlike the List returned past times Arrays.asList(), which doesn't let yous to add together or take elements, the Set returned past times newKeySet() is similar whatever normal Set implementations in addition to allows yous add, take or perform whatever other Set operation. You tin fifty-fifty transcend this Set to a method which is accepting a Set interface every bit a parameter. I'll present this inwards coming example.
Let's the code to genuinely generate a concurrent hash ready inwards Java 8:
You tin run into that yous tin easily add together in addition to take elements from the Set returned past times newKeySet() method. As I said, the Set is backed past times ConcurrentHashMap, which agency multiple readers tin retrieve values at the same fourth dimension in addition to besides multiple writers tin insert values into the ready without blocking, until the read in addition to write functioning is happening inwards the same segment. You tin besides see answer)
What is the divergence betwixt HashSet, TreeSet, in addition to LinkedHashSet inwards Java? (answer)
10 Java HashMap based Interview Questions amongst Answers (list)
What is the divergence betwixt HashMap in addition to LinkedHashMap inwards Java? (answer)
How to form a Map past times values inwards Java 8? (tutorial)
How to form an ArrayList using Comparator inwards Java 8? (tutorial)
What is the divergence betwixt Iterator in addition to ListIterator inwards Java? (answer)
How to take elements from HashMap during iteration inwards Java? (answer)
Thanks for reading this article, if yous similar this article in addition to thence delight part amongst your friends in addition to colleagues. If yous own got whatever query or feedback in addition to thence delight drib a comment.
The Set returned past times the keySet() method is backed past times a ConcurrentHashMap amongst given keys in addition to values are ever Boolean.TRUE. It is besides overloaded to convey initial capacity to avoid internal re-sizing. If yous know, the HashSet class besides internally uses an HashMap where values are ever same, an object, which is possible because Map allows duplicate values.
Unlike the List returned past times Arrays.asList(), which doesn't let yous to add together or take elements, the Set returned past times newKeySet() is similar whatever normal Set implementations in addition to allows yous add, take or perform whatever other Set operation. You tin fifty-fifty transcend this Set to a method which is accepting a Set interface every bit a parameter. I'll present this inwards coming example.
Java Program to practise ConcurrentHashSet
Here is our Java programme to demonstrate how yous tin generate a ConcurrentHashSet inwards JDK 8 past times using the newKeSet() method added into java.util.concurrent.ConcurrentHashMap class. Even though I own got used an object of the concurrent hash map here, it's non required because dissimilar keySet(), the newKeySet() is a static method.Let's the code to genuinely generate a concurrent hash ready inwards Java 8:
import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /* * Java Program to practise ConcurrentHashSet from ConcurrentHashMap * This code requires JDK 8 because nosotros operate newKeySet() method * which is entirely available inwards ConcurrentHashMap class from JDK 8. */ public class Demo { public static void main(String[] args) throws Exception { // measuring 1 - practise a concurrent hashmap (optional) ConcurrentHashMap<String, Integer> wordLengh = new ConcurrentHashMap<>(); // measuring two - practise a ready from map past times using newKeySet() method // furnish size to foreclose resizing in addition to improve performance Set<String> words = ConcurrentHashMap.newKeySet(wordLengh.size()); // measuring three - yous tin add together elements into set words.add("Java"); words.add("JavaScript"); // measuring iv - yous tin take elements from set words.remove("JavaScript"); // measuring five - yous tin besides transcend this ready to whatever method expecting set System.out.println(words); } } Output: [Java]
You tin run into that yous tin easily add together in addition to take elements from the Set returned past times newKeySet() method. As I said, the Set is backed past times ConcurrentHashMap, which agency multiple readers tin retrieve values at the same fourth dimension in addition to besides multiple writers tin insert values into the ready without blocking, until the read in addition to write functioning is happening inwards the same segment. You tin besides see answer)
What is the divergence betwixt HashSet, TreeSet, in addition to LinkedHashSet inwards Java? (answer)
10 Java HashMap based Interview Questions amongst Answers (list)
What is the divergence betwixt HashMap in addition to LinkedHashMap inwards Java? (answer)
How to form a Map past times values inwards Java 8? (tutorial)
How to form an ArrayList using Comparator inwards Java 8? (tutorial)
What is the divergence betwixt Iterator in addition to ListIterator inwards Java? (answer)
How to take elements from HashMap during iteration inwards Java? (answer)
Thanks for reading this article, if yous similar this article in addition to thence delight part amongst your friends in addition to colleagues. If yous own got whatever query or feedback in addition to thence delight drib a comment.
No comments:
Post a Comment