There are multiple ways to iterate over a Map In Java which we will learn with examples in this post.
Using entrySet() Method
The Map interface has a method called entrySet() which returns a collection view of the map as Map.Entry
Set> entries = mapData.entrySet(); for (Map.Entry keyValue: entries) { String key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println("Key = " + key + " | Value = " + value); }
Using keySet() method
We can retrieve all keys at once using the keySet() method in a Set and then iterate it over to get the associated value.
SetallKeys = mapData.keySet(); for (String key: allKeys) { String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value); }
Using values() method
Unlike keySet() method, we can extract all values at once using the values() method. But we can not backtrack their associated keys. So using this method we can get both keys and values together.
CollectionallValues = mapData.values(); for (String value: allValues) { System.out.println("Value = " + value); }
Using iterator() method
We can use the iterator() method that provides an iterator over a collection. The Iterator interface takes the place of the Enumeration interface in the Java Collections Framework. You can use this instead of the ‘for’ loop. You can call the iterator() method in the output of all three methods above.
IteratorallKeysUsingIterator = mapData.keySet().iterator(); while (allKeysUsingIterator.hasNext()) { String key = allKeysUsingIterator.next(); String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value); }
Complete Program
package collectionsExamples; import java.util.*; public class IterateMap { public static void main(String[] args) { //create a map data HashMapmapData = new HashMap<>(); mapData.put("id","1"); mapData.put("name","Amod"); mapData.put("address","New York"); mapData.put("skills","Java Selenium"); mapData.put("mob","969768"); // Iterate using entrySet() method Set > entries = mapData.entrySet(); for(Map.Entry keyValue : entries) { String key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println("Key = " + key + " | Value = " + value); } // Iterate using keySet() method Set allKeys = mapData.keySet(); for(String key : allKeys) { String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value); } // Iterate using values() method Collection allValues = mapData.values(); for(String value : allValues) { System.out.println("Value = " + value); } // Iterate using iterate() method Iterator allKeysUsingIterator = mapData.keySet().iterator(); while(allKeysUsingIterator.hasNext()) { String key = allKeysUsingIterator.next(); String value = mapData.get(key); System.out.println("Key = " + key + " | Value = " + value); } } }
Output
For all ways except the values() method, the output will be as below. For the values() method way we will just get values, not keys.
Key = skills | Value = Java Selenium Key = mob | Value = 969768 Key = address | Value = New York Key = name | Value = Amod Key = id | Value = 1
We can also use lambda function and Stream concepts which I will cover in the next post.
Thanks for reading.