In this post, I show you different ways to iterate over a HashMap in Java with an example.
- Iterating over a HashMap using Java 8 forEach and lambda.
- Iterating over the HashMap's entrySet using iterator().
- Iterating over the HashMap's entrySet using Java 8 forEach and lambda.
- Iterating over the HashMap's entrySet using a simple for-each loop.
- Iterating over the HashMap's keySet.
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("David", 76000.00); employeeSalary.put("John", 120000.00); employeeSalary.put("Mark", 95000.00); employeeSalary.put("Steven", 134000.00); System.out.println("=== Iterating over a HashMap using Java 8 forEach and lambda ==="); employeeSalary.forEach((employee, salary) -> { System.out.println(employee + " => " + salary); }); System.out.println("\n=== Iterating over the HashMap's entrySet using iterator() ==="); Set<Map.Entry<String, Double>> employeeSalaryEntries = employeeSalary.entrySet(); Iterator<Map.Entry<String, Double>> employeeSalaryIterator = employeeSalaryEntries.iterator(); while (employeeSalaryIterator.hasNext()) { Map.Entry<String, Double> entry = employeeSalaryIterator.next(); System.out.println(entry.getKey() + " => " + entry.getValue()); } System.out.println("\n=== Iterating over the HashMap's entrySet using Java 8 forEach and lambda ==="); employeeSalary.entrySet().forEach(entry -> { System.out.println(entry.getKey() + " => " + entry.getValue()); }); System.out.println("\n=== Iterating over the HashMap's entrySet using simple for-each loop ==="); for(Map.Entry<String, Double> entry: employeeSalary.entrySet()) { System.out.println(entry.getKey() + " => " + entry.getValue()); } System.out.println("\n=== Iterating over the HashMap's keySet ==="); employeeSalary.keySet().forEach(employee -> { System.out.println(employee + " => " + employeeSalary.get(employee)); }); } }
Output
=== Iterating over a HashMap using Java 8 forEach and lambda ===
David => 76000.0
John => 120000.0
Mark => 95000.0
Steven => 134000.0
=== Iterating over the HashMap's entrySet using iterator() ===
David => 76000.0
John => 120000.0
Mark => 95000.0
Steven => 134000.0
=== Iterating over the HashMap's entrySet using Java 8 forEach and lambda ===
David => 76000.0
John => 120000.0
Mark => 95000.0
Steven => 134000.0
=== Iterating over the HashMap's entrySet using simple for-each loop ===
David => 76000.0
John => 120000.0
Mark => 95000.0
Steven => 134000.0
=== Iterating over the HashMap's keySet ===
David => 76000.0
John => 120000.0
Mark => 95000.0
Steven => 134000.0
Collection Framework
HashMap
Java
Comments
Post a Comment