6/12/2006

4 Ways to Traverse a Map

Usually I don't need to traverse a java.util.Map, which is meant to be looked up, not iterated through. I traverse a java.util.Map primarily for debugging purposem, to dump its content. In case you do need to, here are 4 ways I've tried, just as examples:

import static java.net.HttpURLConnection.*;
import java.util.*;

public class MapTest {
public static void main(String... args) {
traverseMap();
}

private static void traverseMap() {
Map<Integer, String> data = new HashMap<Integer, String>();
data.put(HTTP_OK, "HTTP_OK");
data.put(HTTP_FORBIDDEN, "HTTP_FORBIDDEN");
data.put(HTTP_NOT_FOUND, "HTTP_NOT_FOUND");

System.out.printf("%nUsing JDK 5 foreach and entry set:%n");
Set<Map.Entry<Integer, String>> entries = data.entrySet();
for(Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.printf("%s = %s%n", key, value);
}

System.out.printf("%nUsing Iterator<Map.Entry> and entry set:%n");
for(Iterator<Map.Entry<Integer, String>> it = entries.iterator(); it.hasNext();) {
Map.Entry<Integer, String> entry = it.next();
Integer key = entry.getKey();
String value = entry.getValue();
System.out.printf("%s = %s%n", key, value);
}

System.out.printf("%nUsing JDK 5 foreach and key set:%n");
for(Integer key : data.keySet()) {
String value = data.get(key);
System.out.printf("%s = %s%n", key, value);
}

System.out.printf("%nUsing traditional Iterator and key set%n");
for(Iterator<Integer> it = data.keySet().iterator(); it.hasNext();) {
Integer key = it.next();
String value = data.get(key);
System.out.printf("%s = %s%n", key, value);
}
}
}
You may have noticed I'm using primitive int numbers (200, 403, 404) as the map key. Thanks to auto-boxing in JDK 5, I can use primitive s and their wrapper types interchangeably. Another useful feature in JDK 5 used here is using static import to import all HTTP status constants from java.net.HttpURLConnection.

To compile and run the test:

$ javac MapTest.java
$ java MapTest

Using JDK 5 foreach and entry set:
200 = HTTP_OK
403 = HTTP_FORBIDDEN
404 = HTTP_NOT_FOUND

Using Iterator<Map.Entry> and entry set:
200 = HTTP_OK
403 = HTTP_FORBIDDEN
404 = HTTP_NOT_FOUND

Using JDK 5 foreach and key set:
200 = HTTP_OK
403 = HTTP_FORBIDDEN
404 = HTTP_NOT_FOUND

Using traditional Iterator and key set
200 = HTTP_OK
403 = HTTP_FORBIDDEN
404 = HTTP_NOT_FOUND
There is some discussion on entrySet vs keySet in the comment section of a previous post: Unnecessary Cast in Map. Basically, using entry set is faster than using key set in traversing a Map. I also learned to use entrySet from there.

14 comments:

Ali Hammad Baig said...

nice tutorial .. helped me alot

Anonymous said...

this does not compile (in JDK 1.7)... a few errors. If you could look into it I'd love it.

javahowto said...

Updated code sample to include the complete test class and output, to use generics collection, and to use printf instead of println for better formatting.

Matthew Doucette said...

Sorry I pasted the wrote code fix... but looks like you fixed it anyway. I meant the map.entry needed to be capitalized into Map.Entry. You can remove that bad comment from me.

I'll sign this one this time with my real name!

javahowto said...

Thanks for spotting this. I guess what happened was, I forgot to escape < and > around Map.Entry, and blogger treated them as an html element and made them all lowercase.

Anonymous said...

Awesome one !

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Dipanwita said...

Great blog. java training in chennai

jvimala said...

Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
Java Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai

keanna said...

With exceedingly prepared experts, useful learning, and of course, our Python training, you will almost certainly break prospective employee meetings and achieve a great deal as a designer.
For More Info:- Python Training in Gurgaon

Adam Schule said...

Here we will use new foreach loop introduced in JDK5 for iterating over any map in java and using KeySet of the map for getting keys. this will iterate through all values of Map and display key and value together. That’s all on multiple ways of looping Map in Java. We have seen exactly 4 examples to iterator on Java Map in a combination of KeySet and EntrySet by using for loop and Iterator. Let me know if you are familiar with any other ways of iterating and getting each key value from Map in Java. Good Examples of iterating hashmap. I think java 5 way of looping hashmap using new for loop is best in terms of cleanliness and readability but Iterator provides in own advantage. as you can remove elements while looping or iterating over hashmap.

IT Technology Updates said...

Cool blog with interesting contents..Really nice..

angularjs Training in Chennai
angularjs Training in Chennai BITA Academy
node.js Training in Chennai BITA Academy
node.js Training in Chennai
Data Science Course Content
Data Science Course in chennai quora
Data Science Course fees in chennai
Blockchain Training in chennai
Blockchain Classroom Training in chennai
Blockchain Training Institute in chennai
Linux Training in chennai
TestComplete Training in chennai

Diyalabs said...

Thanks for sharing such a helpful, and understandable blog. I really enjoyed reading it.

Robots for kids
Robotic Online Classes
Robotics School Projects
Programming Courses Malaysia
Coding courses
Coding Academy
coding robots for kids
Coding classes for kids
Coding For Kids

aditya said...


Very wonderful informative article. I appreciated looking at your article. Very wonderful reveal. I would like to twit this on my followers. Many thanks!

polar covalent bond definition
ground state electron configuration
horizontal asymptote
associative learning
beneficence definition
shaping psychology definition
hyperpyrexia
costochondral separation
why has the kingdom protista been abandoned
analogous structures