HashMap एक क्लास है जो Map इंटरफेस को लागू करती है। यह हैश टेबल पर आधारित है। यह null मान और null कुंजी की अनुमति देता है।
आप HashMap ऑब्जेक्ट में कुंजी-मान जोड़े संग्रहीत कर सकते हैं। एक बार ऐसा करने के बाद आप संबंधित कुंजियों के मान प्राप्त कर सकते हैं लेकिन, कुंजियों के लिए उपयोग किए जाने वाले मान अद्वितीय होने चाहिए।
डुप्लिकेट मान
put कमांड निर्दिष्ट कुंजी के साथ मान को जोड़ता है। यानी यदि हम एक कुंजी-मान जोड़ जोड़ते हैं जहां कुंजी पहले से मौजूद है, तो यह विधि कुंजी के मौजूदा मान को नए मान से बदल देती है,
उदाहरण
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class DuplicatesInHashMap { public static void main(String args[]) { HashMap map = new HashMap(); map.put("Krishna", 9000123456L); map.put("Rama", 9000234567L); map.put("Sita", 9000345678L); map.put("Bhima", 9000456789L); map.put("Yousuf ", 9000456789L); System.out.println("Values Stored . . . . . ."); //Hash map के मान पुनर्प्राप्त करना Iterator it1 = map.entrySet().iterator(); System.out.println("Contents of the hashMap are: "); while(it1.hasNext()){ Map.Entry ele = (Map.Entry) it1.next(); System.out.print(ele.getKey()+": "); System.out.print(ele.getValue()); System.out.println(); } map.put("Bhima", 0000000000L); map.put("Rama", 0000000000L); //Hash map के मान पुनर्प्राप्त करना Iterator it2 = map.entrySet().iterator(); System.out.println("Contents of the hashMap after inserting new key-value pair: "); while(it2.hasNext()){ Map.Entry ele = (Map.Entry) it2.next(); System.out.print(ele.getKey()+": "); System.out.print(ele.getValue()); System.out.println(); } } } आउटपुट
Values Stored . . . . . . Contents of the hashMap are: Yousuf : 9000456789 Krishna : 9000123456 Sita : 9000345678 Rama : 9000234567 Bhima : 9000456789 Contents of the hashMap after inserting new key-value pair: Yousuf : 9000456789 Krishna : 9000123456 Sita : 9000345678 Rama : 0 Bhima : 0