As a Java developer, everyone should know how to Iterate through HashMap, as it will be part of his routine programming. Unlike other Collections, we cannot iterate through HashMap directly we need to get the keySet or entrySet to iterate.
In this article, we will learn about all the different ways of iterating a HashMap in Java.
8 Best ways to Iterate through HashMap in Java
Method 1. Iterate through a HashMap EntrySet using Iterator
Map interface didn’t extend a Collection interface and hence it will not have its own iterator. entrySet() returns a Set and a Set interface which extends the Collection interface and now on top of it, we can use the Iterator.
Now we can get the key-value pair easily using the getKey() and getValue() method.
package com.javainterviewpoint.iterate; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class IterateHashMap1 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); Iterator<Map.Entry<String, String>> entrySet = hm.entrySet().iterator(); while (entrySet.hasNext()) { Map.Entry<String, String> entry = entrySet.next(); System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue()); } } }
Method 2. Iterate through HashMap KeySet using Iterator
The keySet() method returns the Set of all the Keys in the HashMap. Since it is a Set again we can use the Iterator to iterate it.
package com.javainterviewpoint.iterate; import java.util.HashMap; import java.util.Iterator; public class IterateHashMap2 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); Iterator keySetIterator = hm.keySet().iterator(); while (keySetIterator.hasNext()) { String key = keySetIterator.next(); System.out.println("Key : "+key+" Value : "+hm.get(key)); } } }
Method 3. Iterate HashMap using For-each Loop
The For-Each loop is available for all the classes which implement the Iterable interface. entrySet() method returns Set interface, Set interface extends the Collection interface which in turn extends the Iterable Interface.
The for-each loop (or) enhanced for loop in Java will invoke the iterator() method internally.
package com.javainterviewpoint.iterate; import java.util.HashMap; import java.util.Map; import java.util.Set; public class IterateHashMap3 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); Set<Map.Entry<String, String>> entrySet = hm.entrySet(); for(Map.Entry<String, String> entry : entrySet) { System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue()); } } }
Method 4. Iterate a HashMap using For-each Loop [KeySet]
package com.javainterviewpoint.iterate; import java.util.HashMap; import java.util.Set; public class IterateHashMap4 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); Set<String> keySet = hm.keySet(); for(String key : keySet) { System.out.println("Key : "+key+" Value : "+hm.get(key)); } } }
Method 5. Iterating through a HashMap using Lambda Expressions
The forEach() method of the HashMap takes up the BiConsumer functional interface as the argument and hence we can pass it a lambda expression that takes two inputs as argument key and value
package com.javainterviewpoint.iterate; import java.util.HashMap; public class IterateHashMap5 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); hm.forEach((k,v) -> {System.out.println("Key : "+k+" Value : "+v);}); } }
We can also use map.entrySet().forEach() which belongs to the Iterable interface, it takes up Consumer Functional Interface as the argument.
package com.javainterviewpoint.iterate; import java.util.HashMap; public class IterateHashMap6 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); hm.entrySet().forEach((entry) -> {System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue());}); } }
Method 6. Loop through a HashMap using Stream API
The Stream represents a sequence of elements from a source, the source can be a Collection or array which can provide data to a Stream.
In the below code we will get EntrySet from the entrySet() method, from the entrySet we will get the Stream through the stream() method which we will be iterating using forEach(). The entry reference will be further passed to a lambda
package com.javainterviewpoint.iterate; import java.util.HashMap; public class IterateHashMap7 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); hm.entrySet().stream().forEach (entry-> System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue())); } }
Method 7. Using Stream of() method
We will be converting a HashMap into a Stream using the Stream.of() method, on top which we can use the forEach() method to iterate.
package com.javainterviewpoint.iterate; import java.util.HashMap; import java.util.stream.Stream; public class IterateHashMap8 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); Stream.of(hm.entrySet()) .forEach((entry) ->{System.out.println(entry);}); } }
Method 8. Iterate a HashMap using Iterator.forEachRemaining() method
The forEachRemaining() method is newly added to Iterator interface in Java 8. As we have seen it earlier we can get the iterator of a Map through a Set [entrySet()]
package com.javainterviewpoint.iterate; import java.util.HashMap; public class IterateHashMap9 { public static void main(String[] args) { HashMap<String,String> hm = new HashMap<>(); hm.put("Cricket", "Sachin"); hm.put("Football", "Zidane"); hm.put("Tennis", "Federer"); hm.entrySet().iterator().forEachRemaining(entry-> System.out.println("Key : "+entry.getKey()+" Value : "+entry.getValue())); } }
Leave a Reply