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
- Using keyset() and for each loop(Java 5)
- Using keyset() and java Iterator
- Using EntrySet() and for each loop(Java 5)
- 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Â
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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> countryCapitalMap=new HashMap<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 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> 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>> entryIterator=countryCapitalMap.entrySet().iterator(); while(entryIterator.hasNext()) { Entry<string> entry=entryIterator.next(); System.out.println("Country:"+ entry.getKey() +" and Capital:"+entry.getValue()); } System.out.println("-----------------------------"); } } </string></entry></string></string></string> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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 ----------------------------- |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.