Java में आपने Array , ArrayList और LinkedList के बारे में पढ़ा और समझा , आपको याद होगा कि ये classes items को index के according access करते थे और index के according ही items को edit या remove करते थे।


HashMap भी java.util package की एक class है। HashMap items को index की वजाय key : value pair में store करता है , और item को access और modify करने के लिए लिए उसी key का use करते है। इसमें different - different data object को store कर सकते हैं।

Java HashMap Example

File : JavaLinkedList.java

CopyFullscreenClose FullscreenRun
// Import the HashMap class from java.util package.
import java.util.HashMap;
public class HashMapTest {
  public static void main(String[] args) {
    // Create a HashMap object called countries.
    HashMap<String, String> countries = new HashMap<String, String>();

    // Add country : capital as keys : values pair.
    countries.put("India", "New Delhi");
    countries.put("Israel", "Jerusalem");
    countries.put("Russia", "Moscow");
    countries.put("Indonesia", "Jakarta");
    System.out.println(countries);
  }
}
Output
{Israel=Jerusalem, India=New Delhi, Russia=Moscow, Indonesia=Jakarta}

HashMap define समय key और value दोनों का type define करना न भूलें , जैसे कि ऊपर दिए गए example में दिया गया है। Example में <String, String> का मतलब है key और value दोनों ही String हैं।

// to store integer value.
HashMap<String, Integer> data = new HashMap<String, Integer>();
data.put("age", 25);

// Output : {age=25}

इसे आप JavaScript Object , PHP Associative Array और C++ structure के जैसे समझ सकते हैं।

Java access item from HashMap

HashMap से किसी item को access करने के लिए get() method का use किया जाता है , जिसमे हमें item key pass करना पड़ता है।

 System.out.println(countries.get("India"));

// Output:
New Delhi

Java HashMap remove item

HashMap से किसी item को remove करने के लिए remove() method का use किया जाता है , जिसमे हमें item key pass करना पड़ता है।

countries.remove("Indonesia");

Java HashMap remove all item

HashMap से सभी items को remove करने के लिए clear() method का use किया जाता है।

countries.clear();

Java HashMap Size

HashMap की length / size पता करने के लिए size() method का use किया जाता है।

countries.size();

Java Loop Through a HashMap

HashMap के सभी elements को आप for Loop की help से iterate भी कर सकते हैं।

Java HashMap Example

File : JavaLinkedList.java

CopyFullscreenClose FullscreenRun
import java.util.HashMap;
public class HashMapTest {
  public static void main(String[] args) {
    HashMap<String, String> countries = new HashMap<String, String>();
    countries.put("India", "New Delhi");
    countries.put("Israel", "Jerusalem");
    countries.put("Russia", "Moscow");
    countries.put("Indonesia", "Jakarta");
    for (String key : countries.keySet()) {
      System.out.println(key +" : "+ countries.get(key));
    }
  }
}
Output
Israel : Jerusalem
India : New Delhi
Russia : Moscow
Indonesia : Jakarta

Java HashMap keySet()

HashMap class का keySet() method , सभी keys का array return करता है। हालाँकि आप चाहे तो values() method का use करके directly value को भी access कर सकते हैं।

Java HashMap Example

File : JavaLinkedList.java

CopyFullscreenClose FullscreenRun
import java.util.HashMap;
public class HashMapTest {
  public static void main(String[] args) {
    HashMap<String, String> countries = new HashMap<String, String>();
    countries.put("India", "New Delhi");
    countries.put("Israel", "Jerusalem");
    countries.put("Russia", "Moscow");
    countries.put("Indonesia", "Jakarta");
    for (String capital : countries.values()) {
      System.out.println(capital);
    }
  }
}
Output
Jerusalem
New Delhi
Moscow
Jakarta

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers