Java HashMap detailed tutorial

Home - Tutorials - Basics

This is a detailed description about how to use HashMap in Java. Lot of code examples makes it easy to understand it's behaviour.

Tutorial info:

Name:Java HashMap detailed tutorial
Total steps:1
Product:See complete product

Bookmark Java HashMap detailed tutorial

AddThis Social Bookmark Button

Step 1 - How to use Java HashMap

Java HashMap detailed tutorial

HashMap is an implementation of the Map interface. It means that it stores key-value pairs. Both the key and the value also must be an Object. You can not put primitive types into a HashMap, in this cases use their wrapper classes like Integer. Both the key and the value can be null.

Here is a simple example how to create and fill a HashMap. 

  1. HashMap hm = new HashMap();
  3. hm.put("A","Test-A");
  4. hm.put("B","Test-B");
  6. System.out.println(hm.get("A"));

If you use the same key object more than once, then the value field will be overwritten as you can see in this example:

  1. hm.put("A","Test-A");
  2. hm.put("B","Test-B");
  3. System.out.println(hm.get("A"));
  4. hm.put("A","This is an other value");
  5. System.out.println(hm.get("A"));

If you want to get out a key from the HashMap that doesn’t exist then it returns with null. You can also check it with the containsKey() method as presented here:

  1. if (hm.containsKey("NonExistingKey")) {
  2. 	System.out.println("Key exists");
  3. } else {
  4. 	System.out.println("Key doesn't exist");
  5. }

Iterating over a HashMap

In cases when you want to process every item from the HashMap you need to call the entrySet() method on the HashMap to obtain a Set of Map.Entry elements. As next step you can get the Iterator of this step and make a loop to visit every item. Here is a simple demonstration how you can iterate over a HashMap: 

  1. HashMap hm = new HashMap();
  3. hm.put("A","Test-A");
  4. hm.put("B","Test-B");
  5. hm.put("C","Test-C");
  7. Set hmSet = hm.entrySet();
  9. Iterator iter = hmSet.iterator();
  11. while(iter.hasNext()){
  12.     Map.Entry me = (Map.Entry);
  13.     System.out.println(me.getKey() + " -> " + me.getValue() );
  14. }

Or if you simply need the values and not interested the keys you can use this form:

  1. iter = hm.values().iterator();
  2. while(iter.hasNext()){
  3.     System.out.println(;
  4. }

Remove items from the HashMap

If you want to remove an item from the HashMap then you need to use the remove() method, which requires a key object that you want to remove. You can also remove all of the items using the clear() method. 

  1. HashMap hm = new HashMap();
  3. hm.put("A","Test-A");
  4. hm.put("B","Test-B");
  5. hm.put("C","Test-C");
  7. System.out.println("--- The original content ---");
  9. Iterator iter = hm.values().iterator();
  10. while(iter.hasNext()){
  11.     System.out.println(;
  12. }
  14. hm.remove("A");
  15. System.out.println("--- After one item was removed ---");
  17. iter = hm.values().iterator();
  18. while(iter.hasNext()){
  19.     System.out.println(;
  20. }
  22. hm.clear();
  23. System.out.println("--- After all items were removed ---");
  25. iter = hm.values().iterator();
  26. while(iter.hasNext()){
  27.     System.out.println(;
  28. }


Tags: java hashmap, hashmap usage, hashmap example, hashmap code, hashmap iterator

Java HashMap detailed tutorial - Table of contents
Step 1 - How to use Java HashMap

509 Bandwidth Limit Exceeded


Bandwidth Limit Exceeded

The server is temporarily unable to service your request due to bandwidth limit has been reached for this site. Please try again later.

Total time: 0.1461