Different Ways to Iterate over a Map in Java
Explore different ways of iterating over maps in Java to find the most suitable method for your project in this detailed guide at rrtutors.com
To begin with, it is not possible to iterate a Map directly using iterators, because Maps in Java are not collections. The Hava Maps are used to implement the Map interface.
The following are five ways to iterate over HashMap:
a) Using For-Each Loop Method
Using the MapMap.entrySet() method, the SetMap collection-view entry (K, V) of the mappings found in this MapMap is returned. The getKey and getValue methods of Map EntryK allow us to iterate over key-value pairs. This method is the most common and should be used when looping both keys and values.
Example
import java.util.HashMap; import java.util.Map; public class Iterator1 { public static void main(String[] args) { Map<String,String> method1 = new HashMap<String,String>(); method1.put("First", "Java"); method1.put("Second", "Python"); method1.put("Third", "Kotlin"); method1.put("Fourth", "Matlab"); for (Map.Entry<String,String> entry : method1.entrySet()) System.out.println("Rank = " + entry.getKey() + ", language = " + entry.getValue()); } } |
b) Using the keyset () and values () method
Using Map keySet (), you can look at the keys in the MapMap; using Map values (), you can see the values in the MapMap. Using for-each loops, you can loop over keys and values from the MapMap
import java.util.HashMap; import java.util.Map; public class Iterator2 { public static void main(String[] args) { Map<String,String> method2 = new HashMap<String,String>(); method2.put("First", "Java"); method2.put("Second", "Kotlin"); method2.put("Third", "Python"); method2.put("Fourth", "Matlab");
// using keySet() for iteration over keys for (String name : method2.keySet()) System.out.println("Rank: " + name);
// using values() for iteration over values for (String url : method2.values()) System.out.println("language: " + url); } } |
c) Using iterators Method
In some ways, this is similar to the first method. In the first method, we use a for-each loop. Entry (K, V): However, we use iterators here. With an entry [K, V], we can call the iterator to remove () method to remove entries from the MapMap during iteration.
Example
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Iterator3 { public static void main(String[] args) { Map<String,String> method3 = new HashMap<String,String>();
method3.put("First", "Java"); method3.put("Second", "Python"); method3.put("Third", "Kotlin"); method3.put("Fourth", "matlab"); // using iterators Iterator<Map.Entry<String, String>> itr = method3.entrySet().iterator(); while(itr.hasNext()) { Map.Entry<String, String> entry = itr.next(); System.out.println("Rank = " + entry.getKey() + ", Language = " + entry.getValue()); } } } |
d) Using forEach method
This method uses MapMap for.forEach(action) method and lambda expressions to traverse a map. The benefit of using this technique is that it is fast and clean.
Example
import java.util.HashMap; import java.util.Map; public class Iterate4 { public static void main(String[] args) { Map<String,String> method4 = new HashMap<String,String>();
method4.put("First", "Java"); method4.put("Second", "Python"); method4.put("Third", "Kotlin"); method4.put("Fourth", "Matlab"); method4.forEach((k,v) -> System.out.println("Rank = " + k + ", Language = " + v));
} }
|
e) Iterating over keys and searching for values
Using the MapMap.keySet() method, we first loop over keys (then we search for a value for each key using the MapMap.get(key) method). In practice, this method is not used since it is very slow and inefficient since retrieving values by a key could take a great deal of time.
Example
import java.util.HashMap; import java.util.Map; public class Iterate5 { public static void main(String[] args) { Map<String,String> method5 = new HashMap<String,String>();
method5.put("First", "Java"); method5.put("Second", "Python"); method5.put("Third", "Kotlin"); method5.put("Fourth", "Matlab"); for (String name : method5.keySet()) { String url = method5.get(name); System.out.println("Rank = " + name + ", Language = " + url); }
} } |
Conclusion
All Java maps implement the Map interface so that the following techniques can be applied to any map implementation