How to iterate over Map or HashMap in java

In this post, we will see how  can we iterate a map in java. There are four ways of iterating over a map, HashMap or TreeMap.

Java HashMap tutorial:

    HashMap in java How HashMap works in java hash and indexfor method in HashMap hashcode and equals method in java How to sort HashMap by keys and values Difference between HashMap and HashSet Difference between HashMap and Hashtable How to iterate over HashMap
  1. Using keyset() and for each loop(Java 5)
  2. Using keyset() and java Iterator
  3. Using EntrySet() and for each loop(Java 5)
  4. Using EntrySet() and java Iterator
if you remove elements while iterating , then 1st and 3rd option  will throw java.util.ConcurrentModificationException.
If you understand internal working of HashMap, then it may be easier for you to iterate an HashMap

Lets take an example:
1. IterateMapMain.java 
package org.arpit.java2blog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class IterateMapMain {

 public static void main(String args[])
 {
  // HashMap with Country as key and capital as value
  HashMap<String,String> countryCapitalMap=new HashMap<String,String>();
  countryCapitalMap.put("India","Delhi");
  countryCapitalMap.put("Japan","Tokyo");
  countryCapitalMap.put("France","Paris");
  countryCapitalMap.put("Russia","Moscow");

  // Iterating Using keySet() and for each loop
  System.out.println("Iterating Using keySet() and for each loop");
  for (String countryKey:countryCapitalMap.keySet()) {
   System.out.println("Country:"+ countryKey +" and  Capital:"+countryCapitalMap.get(countryKey));

  }
  System.out.println("-----------------------------");

  // Iterating Using keySet() and java iterator
  System.out.println("Iterating Using keySet() and java Iterator");
  Iterator<String> countryKeySetIterator=countryCapitalMap.keySet().iterator();
  while(countryKeySetIterator.hasNext()){
   String countryKey=countryKeySetIterator.next();
   System.out.println("Country:"+ countryKey +" and Capital:"+countryCapitalMap.get(countryKey));

  }
  System.out.println("-----------------------------");

  // Iterating Using entrySet() and for each loop
  System.out.println("Iterating Using entrySet() and for each loop");
  for (Entry<String,String> entry:countryCapitalMap.entrySet()) {
   System.out.println("Country:"+ entry.getKey() +" and  Capital:"+entry.getValue());

  }
  System.out.println("-----------------------------");

  // Iterating Using entrySet() and java iterator
  System.out.println("Iterating Using entrySet() and and java Iterator");
  Iterator<Entry<String,String>> entryIterator=countryCapitalMap.entrySet().iterator();
  while(entryIterator.hasNext())
  {
   Entry<String,String> entry=entryIterator.next();
   System.out.println("Country:"+ entry.getKey() +" and  Capital:"+entry.getValue());

  }
  System.out.println("-----------------------------");

 }

}

Run it and you will get following output:
Iterating Using keySet() and for each loop
Country:France and  Capital:Paris
Country:Russia and  Capital:Moscow
Country:Japan and  Capital:Tokyo
Country:India and  Capital:Delhi
-----------------------------
Iterating Using keySet() and java Iterator
Country:France and Capital:Paris
Country:Russia and Capital:Moscow
Country:Japan and Capital:Tokyo
Country:India and Capital:Delhi
-----------------------------
Iterating Using entrySet() and for each loop
Country:France and  Capital:Paris
Country:Russia and  Capital:Moscow
Country:Japan and  Capital:Tokyo
Country:India and  Capital:Delhi
-----------------------------
Iterating Using entrySet() and and java Iterator
Country:France and  Capital:Paris
Country:Russia and  Capital:Moscow
Country:Japan and  Capital:Tokyo
Country:India and  Capital:Delhi
-----------------------------

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012