Introduction
In this article you will learn how to iterate through HashMap in java. There are several ways of iterating over HashMap. HashMap in Java was introduced in Java 1.2 .
HashMap in Java provides the basic implementation of Map interface of Java. It stores the data in Key-Value pairs. Key is unique in Map whereas value can be duplicate. Values are accessed by using associated key.
There are usually five ways of iterating through a HashMap in Java. In this post, we will discuss all of them.
Iterating over the HashMap’s entrySet using for-each loop
Map.entrySet() method gives a collection of Set<Map.Entry<K, V>>) of the mappings resides in this map.
Therefore, you can iterate over key-value pair by using getKey() and getValue() methods of Map.Entry<K, V>.
If you need both map keys and values in the loop this method should be used. Below is the java program of it.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class IterateOverHashMap { public static void main(String[] args) { Map<String, Double> employeeSalary = new HashMap<>(); employeeSalary.put("Ram", 76000.00); employeeSalary.put("Amit", 120000.00); employeeSalary.put("Mak", 95000.00); employeeSalary.put("Smith", 134000.00); System.out.println("\n=== Iterating over the HashMap's entrySet using for-each loop ==="); for(Map.Entry<String, Double> entry: employeeSalary.entrySet()) { System.out.println(entry.getKey() + " => " + entry.getValue()); } } } Output:- === Iterating over the HashMap's entrySet using for-each loop === Mak => 95000.0 Smith => 134000.0 Amit => 120000.0 Ram => 76000.0
Using keySet() and values() methods
Map.keySet() method gives a Set view of the keys resides in this map.
Map.values() method gives a collection-view of the values resides in this map.
Therefore, if you require only keys or values from the map, we can iterate through keySet or values by using for-each loops. Below is the java program for it.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class IterateOverHashMap { public static void main(String[] args) { Map<String, Double> employeeSalary = new HashMap<>(); employeeSalary.put("Ram", 76000.00); employeeSalary.put("Amit", 120000.00); employeeSalary.put("Mak", 95000.00); employeeSalary.put("Smith", 134000.00); // using keySet() for iteration over keys for (String name : employeeSalary.keySet()) System.out.println("key: " + name); // using values() for iteration over keys for (Double val : employeeSalary.values()) System.out.println("value: " + val); } } Output:- key: Mak key: Smith key: Amit key: Ram value: 95000.0 value: 134000.0 value: 120000.0 value: 76000.0
Iterating over the HashMap’s entrySet using iterator()
Using iterators over Map.Entry<K, V> we can remove entries from the map during the iteration through calling of iterator.remove() method.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class IterateOverHashMap { public static void main(String[] args) { Map<String, Double> employeeSalary = new HashMap<>(); employeeSalary.put("Ram", 76000.00); employeeSalary.put("Amit", 120000.00); employeeSalary.put("Mak", 95000.00); employeeSalary.put("Smith", 134000.00); // using iterators Iterator<Map.Entry<String, Double>> itr = employeeSalary.entrySet().iterator(); while(itr.hasNext()) { Map.Entry<String, Double> entry = itr.next(); System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } } } Output:- Key = Mak, Value = 95000.0 Key = Smith, Value = 134000.0 Key = Amit, Value = 120000.0 Key = Ram, Value = 76000.0
Iterating over keys and looking for values
This is very inefficient way of iterating through HashMap in Java. First we loop over keys(Map.keySet() ) and search for value(Map.get(key) for every key.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class IterateOverHashMap { public static void main(String[] args) { Map<String, Double> employeeSalary = new HashMap<>(); employeeSalary.put("Ram", 76000.00); employeeSalary.put("Amit", 120000.00); employeeSalary.put("Mak", 95000.00); employeeSalary.put("Smith", 134000.00); // looping over keys for (String name : employeeSalary.keySet()) { // search for value Double val= employeeSalary.get(name); System.out.println("Key = " + name + ", Value = " + val); } } } Output:- Key = Mak, Value = 95000.0 Key = Smith, Value = 134000.0 Key = Amit, Value = 120000.0 Key = Ram, Value = 76000.0
Using Java8 forEach(action) method
In Java 8, we can iterate through HashMap in java using Map.forEach(action) method and using lambda expression. This technique is easy and fast.
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class IterateOverHashMap { public static void main(String[] args) { Map<String, Double> employeeSalary = new HashMap<>(); employeeSalary.put("Ram", 76000.00); employeeSalary.put("Amit", 120000.00); employeeSalary.put("Mak", 95000.00); employeeSalary.put("Smith", 134000.00); // forEach(action) method to iterate map employeeSalary.forEach((k,v) -> System.out.println("Key = " + k + ", Value = " + v)); } } Output:- Key = Mak, Value = 95000.0 Key = Smith, Value = 134000.0 Key = Amit, Value = 120000.0 Key = Ram, Value = 76000.0
Conclusion
That’s all folks, we have learnt several ways of iterating through HashMap in java.