Pages

3 Examples to Loop Map in Java - Foreach vs Iterator

There are multiple ways to loop through Map in Java, you can either use a foreach loop or Iterator to traverse Map in Java, but always use either Set of keys or values for iteration. Since Map by default doesn't guarantee any order, any code which assumes a particular order during iteration will fail. You only want to traverse or loop through a Map, if you want to transform each mapping one by one. Now Java 8 release provides a new way to loop through Map in Java using Stream API and forEach method. For now, we will see 3 ways to loop through each element of Map. Though Map is an interface in Java, we often loop through common Map implementation like HashMap, Hashtable, TreeMap and LinkedHashMap. By the way, all the ways of traversing Map discussed in this article is pretty general and valid for any Map implementation, including proprietary and third-party Map classes.
What I mean by looping through Map is getting mappings one at a time, processing them and moving forward. Let's say, we have a Map of Workers, after appraisal every worker has got 8% hike, now our task is to update this Map, so each worker object reflects its new, increased salary.

In order to do this task, we need to iterate through Map, get the Employee object, which is stored as the value. Update Worker with a new salary, and move on. Did you ask about saving it back to Map? no need.

When you retrieve values from Map using get() method, they are not removed from Map, so one reference of the same object is always there in Map until you remove it. That's why, you just need to update object, no need to save it back.

By the way, remember to override equals() and hashCode() method for any object, which you are using as key or value in Map. The internal code of HashMap uses both of these methods to insert and retrieve objects into Map, to learn more see How Map works in Java. Now, Let's see how can we loop through Map in Java.

1) Using for-each loop with Entry Set

The Map provides you a method called entrySet(), which returns a Set of all Map.Entry objects, which are nothing but mapping stored in Map. If you need both key and value to process, then this is the best way, as you get both key and value wrapped in entry, you don't need to call get() method which goes back to Map again to retrieve the value. Now, enhanced for loop of Java 5 makes your job easy, all you need to do is to iterate through that collection as shown in below example :

2) Using Iterator with KeySet

If you don't require values and only needs key then you can use Map.keySet() method to get a set of all keys. Now you can use Iterator to loop through this Set. If you need value any time, then you need to get(key) method of Map, which returns value from Map. This is an additional trip to map, that's why the previous method is better if you need both. Here is how to loop through Map using iterator and key set in Java :

3) Using enhanced foreach loop with values

Now this is the optimal way of looping through Map if you only need values, as it requires less memory than the first approach. Here also we will use enhanced foreach loop to traverse through values, which is a Collection. We can't use plain old for loop with index because Collection doesn't provide any get(index) method, which is only available in List, and it's totally unnecessary to convert Collection to List for iteration only. Here is how to loop Map in Java using enhanced for loop and values method :

Complete Java Program to Loop through Map

Here is full Java code, which you can run in your Java IDE or by using the command prompt. Just copy this code and paste in a Java source file with name MapLoopDemo and save it as .java extension, compile it using "javac" and run it using "java" command.

That's all about how to loop through Map in Java. Traversing is a common task in any programming language and many of them provides easier syntax, contrary to that, In Java you need to write a lot of boilerplate code and our functional intent is hidden between boilerplate code. Java 8 is coming up with more expressive way to do common task in collection using lambda expressions and bulk data operations. For examples task like looping through List, filtering certain elements, transforming one object to another and reducing them into one value will be very easy. So far, you can use above examples to loop through your Map in Java. All examples are valid for any Map implementations including HashMap, Hashtable, TreeMap and LinkedHashMap.

3 comments:

While looping over Map you must remember that Map doesn't guarnatee any order, except TreeMap which guaranteeds sorting order and LinkedHashMap which keeps entries in the order they were inserted into Map. Other than that no Map guarantee any order, not even HashMap. For simple read only iteration the techniques you mentioned is sufficient, especially the one which gets entrySet() and then loop over Map using foreach loop. But, if you have to remove entries during iteration, there is no choice other than using Iterator. Also note, since HashMap is not synchronized, you may be iterating over stale mappings if there is another thread which is modifying Map in background. ConcurrentModificationException is also on the cards.