Wednesday, 20 July 2016

Iterate Over a HashMap in Java

How to iterate over a HashMap in Java

Iterating over a HashMap in Java is a very common task but can be tricky sometimes
since it uses the Entry object (java.util.Map.Entry) which we may not be familiar with readily.

Whenever we need to "Iterate over a HashMap" we need to call the entrySet() method over the map
object and then only we can iterate over it.


This is best achieved in the following two ways.
Supposing we have a Hashmap like like this.


Map<String,String> timeZones=new HashMap<String, String>();
 
timeZones.put(" (GMT-12:00) ", " International Date Line West ");
timeZones.put(" (GMT-11:00) ", " Midway Island ");
timeZones.put(" (GMT-10:00) ", " Hawaii Time ");
timeZones.put(" (GMT-09:00) ", " Alaska Time ");
timeZones.put(" (GMT-08:00) ", " Pacific Time (US and Canada) ");


Method 1: Using For Each Loop to iterate over a HashMap

for(Map.Entry timeZone : timeZones.entrySet()){

    System.out.println(timeZone.getKey() + " is the " + timeZone.getValue());

  }


Method 2: Using Iterator and While Loop to iterate over a HashMap

 while (it.hasNext()) {
  
  Entry timeZone = (Entry)it.next();
  System.out.println(timeZone.getKey() + " is the " + timeZone.getValue());
  it.remove(); 
    }


Output:

 (GMT-08:00)  is the  Pacific Time (US and Canada) 
 (GMT-12:00)  is the  International Date Line West 
 (GMT-09:00)  is the  Alaska Time 
 (GMT-10:00)  is the  Hawaii Time 
 (GMT-11:00)  is the  Midway Island


Well in both cases the result is he same. Its depends on the taste and the situation
to decide which one of the method you want to use.
Going a little deeper we can look into other requirements of HashMap iteration.





Iterate over a HashMap Values/ How to iterate over the values in a HashMap.

Well you may very well come across a situation where you only need to iterate over HashMap values.
This can be easily achieved in the following two ways.

 for(String timeZone : timeZones.values()){

  System.out.println(timeZone);
 }


OR,


Iterator it = timeZones.values().iterator();

 while (it.hasNext()) {
  String timeZone = (String) it.next();
  System.out.println(timeZone);
  it.remove();
 }


Output:

 Pacific Time (US and Canada) 
 International Date Line West 
 Alaska Time 
 Hawaii Time 
 Midway Island 






Iterate over a HashMap Keys/ How to iterate over the Keys in a HashMap.

You may also need to iterate over HashMap keys. This HashMap iteration is shown below.

 for (String timeZone : timeZones.keySet()) {

  System.out.println(timeZone);
 }


OR,


Iterator it = timeZones.keySet().iterator();

 while (it.hasNext()) {
  String timeZone = (String) it.next();
  System.out.println(timeZone);
  it.remove();
 }


Output:

 (GMT-08:00) 
 (GMT-12:00) 
 (GMT-09:00) 
 (GMT-10:00) 
 (GMT-11:00) 


No comments:

Post a Comment

Coder Magnet
CoderMagnet is full of resources from our daily development activities. It has solutions for common problematic scenarios in technologies like Java 8, AEM, JCR and also occasionally gives you tips on Blogger as well.