जावा हाशपैप (उदाहरणों के साथ)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से जावा हाशप क्लास और इसके विभिन्न ऑपरेशनों के बारे में जानेंगे।

HashMapजावा संग्रह रूपरेखा का वर्ग हैश तालिका डेटा संरचना की कार्यक्षमता प्रदान करता है।

यह कुंजी / मूल्य जोड़े में तत्वों को संग्रहीत करता है । यहां, कुंजियां विशिष्ट पहचानकर्ता हैं जिनका उपयोग मानचित्र पर प्रत्येक मूल्य को जोड़ने के लिए किया जाता है ।

HashMapवर्ग मानचित्र इंटरफ़ेस लागू करता है।

जावा हाशपप कार्यान्वयन

एक HashMap बनाएँ

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

 // hashMap creation with 8 capacity and 0.6 load factor HashMap numbers = new HashMap();

उपरोक्त कोड में, हमने एक हैशमैप नाम संख्याएँ बनाई हैं। यहाँ, K प्रमुख प्रकार का प्रतिनिधित्व करता है और V मानों के प्रकार का प्रतिनिधित्व करता है। उदाहरण के लिए,

 HashMap numbers = new HashMap();

यहाँ, कुंजियों का Stringप्रकार है और मूल्यों का प्रकार है Integer

उदाहरण 1: जावा में HashMap बनाएँ

 import java.util.HashMap; class Main ( public static void main(String() args) ( // create a hashmap HashMap languages = new HashMap(); // add elements to hashmap languages.put("Java", 8); languages.put("JavaScript", 1); languages.put("Python", 3); System.out.println("HashMap: " + languages); ) )

आउटपुट

 HashMap: (जावा = 8, जावास्क्रिप्ट = 1, पायथन = 3)

उपरोक्त उदाहरण में, हमने एक HashMapनामित भाषा बनाई है ।

यहां, हमने put()हैशमैप में तत्वों को जोड़ने के लिए विधि का उपयोग किया है । हम put()इस ट्यूटोरियल में बाद में विधि के बारे में अधिक जानेंगे ।

Java HashMap पर मूल संचालन

HashMapवर्ग HashMaps पर विभिन्न कार्यों का प्रदर्शन करने के लिए विभिन्न तरीकों प्रदान करता है। हम इस ट्यूटोरियल में कुछ सामान्य रूप से उपयोग की जाने वाली सरणी सूची देखेंगे:

  • तत्वों को जोड़ें
  • तत्वों को एक्सेस करें
  • तत्वों को बदलें
  • तत्वों को हटा दें

1. तत्वों को हाशप में जोड़ें

हैशमैप में एक एकल तत्व जोड़ने के लिए, हम कक्षा की put()विधि का उपयोग करते हैं HashMap। उदाहरण के लिए,

 import java.util.HashMap; class Main ( public static void main(String() args) ( // create a hashmap HashMap numbers = new HashMap(); System.out.println("Initial HashMap: " + numbers); // put() method to add elements numbers.put("One", 1); numbers.put("Two", 2); numbers.put("Three", 3); System.out.println("HashMap after put(): " + numbers); ) )

आउटपुट

 आरंभिक हैशमैप: () पुट के बाद हैशमैप (): (एक = 1, दो = 2, तीन = 3)

उपरोक्त उदाहरण में, हमने एक HashMapनामांकित संख्याएँ बनाई हैं । यहां, हमने put()तत्वों को संख्याओं में जोड़ने के लिए विधि का उपयोग किया है ।

कथन पर ध्यान दें,

 numbers.put("One", 1);

यहां, हम Stringमान एक को कुंजी और Integerमान 1 को put()विधि के मान के रूप में पास कर रहे हैं ।

अनुशंसित रीडिंग

  • जावा हैशपॅट ()
  • Java HashMap putAll ()
  • जावा हैशपॉट putIfAbsent ()

2. HashMap तत्वों तक पहुँचें

हम get()हैशमैप से मूल्य तक पहुंचने के लिए विधि का उपयोग कर सकते हैं । उदाहरण के लिए,

 import java.util.HashMap; class Main ( public static void main(String() args) ( HashMap languages = new HashMap(); languages.put(1, "Java"); languages.put(2, "Python"); languages.put(3, "JavaScript"); System.out.println("HashMap: " + languages); // get() method to get value String value = languages.get(1); System.out.println("Value at index 1: " + value); ) )

आउटपुट

 HashMap: (1 = जावा, 2 = पायथन, 3 = जावास्क्रिप्ट) मूल्य सूचकांक 1 पर: जावा

उपरोक्त उदाहरण में, अभिव्यक्ति पर ध्यान दें,

 languages.get(1);

यहां, get()विधि कुंजी को उसके तर्क के रूप में लेती है और कुंजी से संबंधित संबंधित मान लौटाती है ।

हम यह भी उपयोग कर सकते हैं कुंजी , मूल्यों , और कुंजी / मान का उपयोग कर सेट विचारों के रूप में hashmap के जोड़े keySet(), values(), और entrySet()क्रमशः तरीकों। उदाहरण के लिए,

 import java.util.HashMap; class Main ( public static void main(String() args) ( HashMap languages = new HashMap(); languages.put(1, "Java"); languages.put(2, "Python"); languages.put(3, "JavaScript"); System.out.println("HashMap: " + languages); // return set view of keys // using keySet() System.out.println("Keys: " + languages.keySet()); // return set view of values // using values() System.out.println("Values: " + languages.values()); // return set view of key/value pairs // using entrySet() System.out.println("Key/Value mappings: " + languages.entrySet()); ) )

आउटपुट

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

उपरोक्त उदाहरण में, हमने भाषाओं के नाम से एक हैशमैप बनाया है। यहाँ, हम हैशमैप से कुंजियों , मानों और की / वैल्यू मैपिंग को एक्सेस कर रहे हैं ।

अनुशंसित रीडिंग

  • जावा हैशपॉप मिलता है ()
  • जावा हैशमैप getOrDefault ()
  • जावा हैशपैड कीसेट ()
  • जावा हाशपैप मूल्य ()
  • Java HashMap प्रविष्टिसेट ()

3. HashMap मान बदलें

हम replace()हैशमैप में एक कुंजी के साथ जुड़े मूल्य को बदलने के लिए विधि का उपयोग कर सकते हैं । उदाहरण के लिए,

 import java.util.HashMap; class Main ( public static void main(String() args) ( HashMap languages = new HashMap(); languages.put(1, "Java"); languages.put(2, "Python"); languages.put(3, "JavaScript"); System.out.println("Original HashMap: " + languages); // change element with key 2 languages.replace(2, "C++"); System.out.println("HashMap using replace(): " + languages); ) )

आउटपुट

 मूल HashMap: (1 = जावा, 2 = पायथन, 3 = जावास्क्रिप्ट) HashMap प्रतिस्थापन का उपयोग कर (): (1 = जावा, 2 = C ++, 3 = जावास्क्रिप्ट)

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

 languages.replace(2, "C++");

यहां, हम कुंजी 2 द्वारा निर्दिष्ट मान को नए मान C ++ के साथ बदल रहे हैं ।

The HashMap class also provides some variations of the replace() method. To learn more, visit

  • Java HashMap replace()
  • Java HashMap replaceAll()

4. Remove HashMap Elements

To remove elements from a hashmap, we can use the remove() method. For example,

 import java.util.HashMap; class Main ( public static void main(String() args) ( HashMap languages = new HashMap(); languages.put(1, "Java"); languages.put(2, "Python"); languages.put(3, "JavaScript"); System.out.println("HashMap: " + languages); // remove element associated with key 2 String value = languages.remove(2); System.out.println("Removed value: " + value); System.out.println("Updated HashMap: " + languages); ) )

Output

 HashMap: (1=Java, 2=Python, 3=JavaScript) Removed value: Python Updated HashMap: (1=Java, 3=JavaScript)

Here, the remove() method takes the key as its parameter. It then returns the value associated with the key and removes the entry.

We can also remove the entry only under certain conditions. For example,

 remove(2, "C++");

Here, the remove() method only removes the entry if the key 2 is associated with the value C++. Since 2 is not associated with C++, it doesn't remove the entry.

To learn more, visit Java HashMap remove().

Other Methods of HashMap

Method Description
clear() removes all mappings from the HashMap
compute() computes a new value for the specified key
computeIfAbsent() computes value if a mapping for the key is not present
computeIfPresent() computes a value for mapping if the key is present
merge() merges the specified mapping to the HashMap
clone() makes the copy of the HashMap
containsKey() checks if the specified key is present in Hashmap
containsValue() checks if Hashmap contains the specified value
size() returns the number of items in HashMap
isEmpty() checks if the Hashmap is empty

Iterate through a HashMap

To iterate through each entry of the hashmap, we can use Java for-each loop. We can iterate through keys only, vales only, and key/value mapping. For example,

 import java.util.HashMap; import java.util.Map.Entry; class Main ( public static void main(String() args) ( // create a HashMap HashMap languages = new HashMap(); languages.put(1, "Java"); languages.put(2, "Python"); languages.put(3, "JavaScript"); System.out.println("HashMap: " + languages); // iterate through keys only System.out.print("Keys: "); for (Integer key : languages.keySet()) ( System.out.print(key); System.out.print(", "); ) // iterate through values only System.out.print("Values: "); for (String value : languages.values()) ( System.out.print(value); System.out.print(", "); ) // iterate through key/value entries System.out.print("Entries: "); for (Entry entry : languages.entrySet()) ( System.out.print(entry); System.out.print(", "); ) ) )

Output

 HashMap: (1=Java, 2=Python, 3=JavaScript) Keys: 1, 2, 3, Values: Java, Python, JavaScript, Entries: 1=Java, 2=Python, 3=JavaScript,

Note that we have used the Map.Entry in the above example. It is the nested class of the Map interface that returns a view (elements) of the map.

We first need to import the java.util.Map.Entry package in order to use this class.

This nested class returns a view (elements) of the map.

Creating HashMap from Other Maps

In Java, we can also create a hashmap from other maps. For example,

 import java.util.HashMap; import java.util.TreeMap; class Main ( public static void main(String() args) ( // create a treemap TreeMap evenNumbers = new TreeMap(); evenNumbers.put("Two", 2); evenNumbers.put("Four", 4); System.out.println("TreeMap: " + evenNumbers); // create hashmap from the treemap HashMap numbers = new HashMap(evenNumbers); numbers.put("Three", 3); System.out.println("HashMap: " + numbers); ) )

Output

 TreeMap: (Four=4, Two=2) HashMap: (Two=2, Three=3, Four=4)

In the above example, we have created a TreeMap named evenNumbers. Notice the expression,

 numbers = new HashMap(evenNumbers)

Here, we are creating a HashMap named numbers using the TreeMap. To learn more about treemap, visit Java TreeMap.

Note: While creating a hashmap, we can include optional parameters: capacity and load factor. For example,

 HashMap numbers = new HashMap(8, 0.6f);

Here,

  • 8 (capacity is 8) - This means it can store 8 entries.
  • 0.6f (load factor is 0.6) - This means whenever our hash table is filled by 60%, the entries are moved to a new hash table double the size of the original hash table.

यदि वैकल्पिक मापदंडों का उपयोग नहीं किया जाता है, तो डिफ़ॉल्ट क्षमता 16 होगी और डिफ़ॉल्ट लोड कारक 0.75 होगा ।

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