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
Category:Basics
Date:2011-04-13
Level:Beginner
Product:See complete product
Viewed:12741

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. 

Code:
  1. HashMap hm = new HashMap();
  2.  
  3. hm.put("A","Test-A");
  4. hm.put("B","Test-B");
  5.  
  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:

Code:
  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:

Code:
  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: 

Code:
  1. HashMap hm = new HashMap();
  2.  
  3. hm.put("A","Test-A");
  4. hm.put("B","Test-B");
  5. hm.put("C","Test-C");
  6.  
  7. Set hmSet = hm.entrySet();
  8.  
  9. Iterator iter = hmSet.iterator();
  10.  
  11. while(iter.hasNext()){
  12.     Map.Entry me = (Map.Entry)iter.next();
  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:

Code:
  1. iter = hm.values().iterator();
  2. while(iter.hasNext()){
  3.     System.out.println(iter.next());
  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. 

Code:
  1. HashMap hm = new HashMap();
  2.  
  3. hm.put("A","Test-A");
  4. hm.put("B","Test-B");
  5. hm.put("C","Test-C");
  6.  
  7. System.out.println("--- The original content ---");
  8.  
  9. Iterator iter = hm.values().iterator();
  10. while(iter.hasNext()){
  11.     System.out.println(iter.next());
  12. }
  13.  
  14. hm.remove("A");
  15. System.out.println("--- After one item was removed ---");
  16.  
  17. iter = hm.values().iterator();
  18. while(iter.hasNext()){
  19.     System.out.println(iter.next());
  20. }
  21.  
  22. hm.clear();
  23. System.out.println("--- After all items were removed ---");
  24.  
  25. iter = hm.values().iterator();
  26. while(iter.hasNext()){
  27.     System.out.println(iter.next());
  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



Total time: 0.1651