Java ConcurrentHashMap

इस ट्यूटोरियल में, हम उदाहरणों की सहायता से Java ConcurrentHashMap वर्ग और उसके संचालन के बारे में जानेंगे।

ConcurrentHashMapजावा संग्रह ढांचे का वर्ग एक धागा-सुरक्षित मानचित्र प्रदान करता है। यही है, कई थ्रेड्स मानचित्र में प्रविष्टियों की स्थिरता को प्रभावित किए बिना एक बार में मानचित्र तक पहुंच सकते हैं।

यह समवर्ती मैप इंटरफ़ेस लागू करता है।

एक समवर्ती बनाएं

समवर्ती हैशमैप बनाने के लिए, हमें java.util.concurrent.ConcurrentHashMapपहले पैकेज को आयात करना होगा । एक बार जब हम पैकेज को इम्पोर्ट करते हैं, तो यहां बताया गया है कि हम जावा में समवर्ती हैशमैप कैसे बना सकते हैं।

 // ConcurrentHashMap with capacity 8 and load factor 0.6 ConcurrentHashMap numbers = new ConcurrentHashMap(8, 0.6f); 

उपरोक्त कोड में, हमने एक समवर्ती हैशमैप नाम संख्याएं बनाई हैं।

यहाँ,

  • कुंजी - मानचित्र में प्रत्येक तत्व (मूल्य) को जोड़ने के लिए उपयोग किया जाने वाला एक विशिष्ट पहचानकर्ता
  • मूल्य - एक नक्शे में कुंजियों से जुड़े तत्व

भाग को नोटिस करें new ConcurrentHashMap(8, 0.6)। यहां, पहला पैरामीटर क्षमता है और दूसरा पैरामीटर लोडफैक्टर है

  • क्षमता - इस मानचित्र की क्षमता 8. है। यह 8 प्रविष्टियों को संग्रहीत कर सकता है।
  • loadFactor - इस मानचित्र का भार कारक 0.6 है। इसका मतलब है, जब भी हमारी हैश तालिका 60% से भर जाती है, तो प्रविष्टियां मूल हैश तालिका के आकार के दोगुने के एक नए हैश तालिका में ले जाया जाता है।

डिफ़ॉल्ट क्षमता और लोड कारक

अपनी क्षमता और भार कारक को परिभाषित किए बिना एक समवर्ती हैशमप बनाना संभव है। उदाहरण के लिए,

 // ConcurrentHashMap with default capacity and load factor ConcurrentHashMap numbers1 = new ConcurrentHashMap(); 

डिफ़ॉल्ट रूप से,

  • मानचित्र की क्षमता 16 होगी
  • लोड फैक्टर 0.75 होगा

अन्य मैप्स से समवर्ती हाशिएप बनाना

यहां बताया गया है कि हम एक समवर्ती हैशमैप बना सकते हैं जिसमें अन्य मानचित्रों के सभी तत्व हैं।

 import java.util.concurrent.ConcurrentHashMap; import java.util.HashMap; class Main ( public static void main(String() args) ( // Creating a hashmap of even numbers HashMap evenNumbers = new HashMap(); evenNumbers.put("Two", 2); evenNumbers.put("Four", 4); System.out.println("HashMap: " + evenNumbers); // Creating a concurrent hashmap from other map ConcurrentHashMap numbers = new ConcurrentHashMap(evenNumbers); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); ) ) 

आउटपुट

 HashMap: (चार = 4, दो = 2) समवर्ती 

समवर्ती हाशपा के तरीके

ConcurrentHashMapवर्ग तरीकों कि नक्शे पर हमारे लिए विभिन्न कार्य करने की अनुमति प्रदान करता है।

समवर्ती हाशिए पर तत्वों को सम्मिलित करें

  • put() - नक्शे में निर्दिष्ट कुंजी / मूल्य मानचित्रण सम्मिलित करता है
  • putAll() - निर्दिष्ट नक्शे से इस नक्शे के लिए सभी प्रविष्टियों को सम्मिलित करता है
  • putIfAbsent() - यदि मानचित्र में निर्दिष्ट कुंजी मौजूद नहीं है तो मानचित्र में निर्दिष्ट कुंजी / मान मानचित्रण सम्मिलित करता है

उदाहरण के लिए,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( // Creating ConcurrentHashMap of even numbers ConcurrentHashMap evenNumbers = new ConcurrentHashMap(); // Using put() evenNumbers.put("Two", 2); evenNumbers.put("Four", 4); // Using putIfAbsent() evenNumbers.putIfAbsent("Six", 6); System.out.println("ConcurrentHashMap of even numbers: " + evenNumbers); //Creating ConcurrentHashMap of numbers ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); // Using putAll() numbers.putAll(evenNumbers); System.out.println("ConcurrentHashMap of numbers: " + numbers); ) ) 

आउटपुट

 सम संख्याओं का समवर्ती हाशपा 

समवर्ती हाशिए तत्वों तक पहुँचें

1. प्रविष्टि का उपयोग करना (), कीसेट () और मान ()

  • entrySet() - नक्शे के सभी कुंजी / मान मानचित्रण का एक सेट लौटाता है
  • keySet() - नक्शे की सभी चाबियों का एक सेट देता है
  • values() - नक्शे के सभी मूल्यों का एक सेट देता है

उदाहरण के लिए,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); // Using entrySet() System.out.println("Key/Value mappings: " + numbers.entrySet()); // Using keySet() System.out.println("Keys: " + numbers.keySet()); // Using values() System.out.println("Values: " + numbers.values()); ) ) 

आउटपुट

 समवर्तीHashMap: (एक = 1, दो = 2, तीन = 3) कुंजी / मूल्य मैपिंग: (एक = 1, दो = 2, तीन = 3) कुंजी: (एक, दो, तीन) मान: (1, 2, 3) ) 

2. का उपयोग कर () और getOrDefault ()

  • get()- निर्दिष्ट कुंजी के साथ जुड़े मूल्य देता है। nullअगर चाबी नहीं मिली तो वापस लौटता है।
  • getOrDefault()- निर्दिष्ट कुंजी के साथ जुड़े मूल्य देता है। यदि कुंजी नहीं मिली है तो निर्दिष्ट डिफ़ॉल्ट मान लौटाता है।

उदाहरण के लिए,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); // Using get() int value1 = numbers.get("Three"); System.out.println("Using get(): " + value1); // Using getOrDefault() int value2 = numbers.getOrDefault("Five", 5); System.out.println("Using getOrDefault(): " + value2); ) ) 

आउटपुट

 समवर्ती हाशिए: (एक = 1, दो = 2, तीन = 3) प्राप्त करें (): 3 का उपयोग कर getOrDefault (): 5 

समवर्ती हाशिए तत्वों को निकालें

  • remove(key) - रिटर्न और मानचित्र से निर्दिष्ट कुंजी के साथ जुड़े प्रविष्टि को हटाता है
  • remove(key, value) - removes the entry from the map only if the specified key mapped to the specified value and return a boolean value

For example,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); // remove method with single parameter int value = numbers.remove("Two"); System.out.println("Removed value: " + value); // remove method with two parameters boolean result = numbers.remove("Three", 3); System.out.println("Is the entry (Three=3) removed? " + result); System.out.println("Updated ConcurrentHashMap: " + numbers); ) ) 

Output

 ConcurrentHashMap: (One=1, Two=2, Three=3) Removed value: 2 Is the entry (Three=3) removed? True Updated ConcurrentHashMap: (One=1) 

Bulk ConcurrentHashMap Operations

The ConcurrentHashMap class provides different bulk operations that can be applied safely to concurrent maps.

1. forEach() Method

The forEach() method iterates over our entries and executes the specified function.

It includes two parameters.

  • parallelismThreshold - It specifies that after how many elements operations in a map are executed in parallel.
  • transformer - This will transform the data before the data is passed to the specified function.

For example,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); // forEach() without transformer function numbers.forEach(4, (k, v) -> System.out.println("key: " + k + " value: " + v)); // forEach() with transformer function System.out.print("Values are "); numbers.forEach(4, (k, v) -> v, (v) -> System.out.print(v + ", ")); ) ) 

Output

 ConcurrentHashMap: (One = 1, Two = 2, Three = 3) key: One value: 1 key: Two value: 2 key: Three value: 3 Values are 1, 2, 3, 

In the above program, we have used parallel threshold 4. This means if the map contains 4 entries, the operation will be executed in parallel.

Variation of forEach() Method

  • forEachEntry() - executes the specified function for each entry
  • forEachKey() - executes the specified function for each key
  • forEachValue() - executes the specified function for each value

2. search() Method

The search() method searches the map based on the specified function and returns the matched entry.

Here, the specified function determines what entry is to be searched.

It also includes an optional parameter parallelThreshold. The parallel threshold specifies that after how many elements in the map the operation is executed in parallel.

For example,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); // Using search() String key = numbers.search(4, (k, v) -> (return v == 3 ? k: null;)); System.out.println("Searched value: " + key); ) ) 

Output

 ConcurrentHashMap: (One=1, Two=2, Three=3) Searched value: Three 

Variants of search() Method

  • searchEntries() - search function is applied to key/value mappings
  • searchKeys() - search function is only applied to the keys
  • searchValues() - search function is only applied to the values

3. reduce() Method

The reduce() method accumulates (gather together) each entry in a map. This can be used when we need all the entries to perform a common task, like adding all the values of a map.

It includes two parameters.

  • parallelismThreshold - It specifies that after how many elements, operations in a map are executed in parallel.
  • transformer - This will transform the data before the data is passed to the specified function.

For example,

 import java.util.concurrent.ConcurrentHashMap; class Main ( public static void main(String() args) ( ConcurrentHashMap numbers = new ConcurrentHashMap(); numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("ConcurrentHashMap: " + numbers); // Using search() int sum = numbers.reduce(4, (k, v) -> v, (v1, v2) -> v1 + v2); System.out.println("Sum of all values: " + sum); ) ) 

Output

 ConcurrentHashMap: (One=1, Two=2, Three=3) Sum of all values: 6 

In the above program, notice the statement

 numbers.reduce(4, (k, v) -> v, (v1, v2) -> v1+v2); 

Here,

  • 4 is a parallel threshold
  • (k, v) -> v is a transformer function. It transfers the key/value mappings into values only.
  • (v1, v2) -> v1+v2 is a reducer function. It gathers together all the values and adds all values.

Variants of reduce() Method

  • reduceEntries() - returns the result of gathering all the entries using the specified reducer function
  • reduceKeys() - returns the result of gathering all the keys using the specified reducer function
  • reduceValues() - returns the result of gathering all the values using the specified reducer function

ConcurrentHashMap vs HashMap

Here are some of the differences between ConcurrentHashMap and HashMap,

  • ConcurrentHashMap is a thread-safe collection. That is, multiple threads can access and modify it at the same time.
  • ConcurrentHashMap provides methods for bulk operations like forEach(), search() and reduce().

Why ConcurrentHashMap?

  • ConcurrentHashMapवर्ग एक से अधिक थ्रेड इसकी प्रविष्टियों समवर्ती का उपयोग करने की अनुमति देता है।
  • डिफ़ॉल्ट रूप से, समवर्ती हैशमैप को 16 खंडों में विभाजित किया जाता है । यही कारण है कि एक ही समय में 16 धागे को मानचित्र को समवर्ती रूप से संशोधित करने की अनुमति है। हालाँकि, थ्रेड्स की कोई भी संख्या एक बार में मानचित्र तक पहुँच सकती है।
  • putIfAbsent()विधि नक्शे में प्रवेश पर हावी नहीं होगा यदि निर्दिष्ट कुंजी पहले से मौजूद।
  • यह अपना खुद का सिंक्रनाइज़ेशन प्रदान करता है।

दिलचस्प लेख...