If you have a HashMap
, what is the best way to iterate over all the elements in the Map
?
Java has a couple of solutions we can use to iterate over just the map keys, the values, or each key-value entry. In the approaches below, we will only consider iterating over map entries that will allow you to access both the map keys and map values during the iteration.
Perhaps the most straightforward approach to iterating over a HashMap is to use a for-each loop to iterate over each entry. Using the HashMap.entrySet()
will return a set view of the mappings or entries
in the HashMap.
import java.util.HashMap; import java.util.Map; public class Example { public static void main(String[] args) { Map<String, String> myMap = new HashMap<>() {{ put("a", "b"); put("c", "d"); }}; iterateUsingForEach(myMap); } public static void iterateUsingForEach(Map<String, String> map) { for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); System.out.println("Key=" + key + ", Value=" + value); } } }
This next approach uses the same for-each syntax as before, but this time we can make use of an Iterator
. Since the HashMap.entrySet()
method returns a Set
, which extends the Collection
interface, we can use the Iterator
instance returned by Map.entrySet().iterator
.
public void iterateUsingForEachIterator(Map<String, String> map) { Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); String key = entry.getKey(); String value = entry.getValue(); System.out.println("Key=" + key + ", Value=" + value); } }
In this approach, we make use of lambda expressions, which have been available in Java since version 8. A lambda expression operates on input parameters and returns a value. The lambda expression solution to this problem does not need each key-value entry to be converted into an entry set.
public void iterateUsingLambda(Map<String, String> map) { map.forEach((key, value) -> { System.out.println("Key=" + key + ", Value=" + value); }); }
In conclusion, we have looked at three approaches to iterating over a HashMap. Each approach is useful for retrieving both the map keys and the map values. We also looked at using lambdas, which use fewer lines of code to achieve the same iteration as non-lambda approaches.