How to Iterate (Loop) Hashmap in Java With Example
Deepak
Dec 20, 2024
Hashmap can be looped through by using
- For Loop Iteration using Iterator
- While Loop iteration with Iterator
- forEach Loop
- Stream
Lets understand with following example one by one
Scroll for More Useful Information and Relevant FAQs
Explore Hashmap for Loop Iteration with example
/*
* For Loop Iteration using Iterator
*
*/
package javaapplication;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class JavaApplication {
public static void main(String[] args) {
// HashMap object with list of employees with their name and department
HashMap<String, String> employees = new HashMap<String, String>();
// keys and values (Name, Department)
employees.put("John", "HR");
employees.put("Eric", "IT");
employees.put("Joe", "LEGAL");
for (Iterator<Map.Entry<String, String>> emp = employees.entrySet().iterator();
emp.hasNext();) {
Map.Entry<String, String> data = emp.next();
System.out.format("key (Name): %s, value (Department): %s %n", data.getKey(), data.getValue());
}
}
}
Output once run this code:
key (Name): Joe, value (Department): LEGAL
key (Name): Eric, value (Department): IT
key (Name): John, value (Department): HR
Hashmap While Loop traversing using Iterator
package javaapplication;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class JavaApplication {
public static void main(String[] args) {
// HashMap object with list of employees with their name and department
HashMap<String, String> employees = new HashMap<String, String>();
// keys and values (Name, Department)
employees.put("John", "HR");
employees.put("Eric", "IT");
employees.put("Joe", "LEGAL");
Iterator<Map.Entry<String, String>> emp = employees.entrySet().iterator();
while (emp.hasNext()) {
Map.Entry<String, String> data = emp.next();
System.out.format("key (Name): %s, value (Department): %s %n", data.getKey(), data.getValue());
}
}
}
While Loop code output:
key (Name): Joe, value (Department): LEGAL
key (Name): Eric, value (Department): IT
key (Name): John, value (Department): HR
Iterate Hashmap key and values using forEach Loop iteration
package javaapplication;
import java.util.Map;
import java.util.HashMap;
public class JavaApplication {
public static void main(String[] args) {
// HashMap object with list of employees with their name and department
HashMap<String, String> employees = new HashMap<String, String>();
// keys and values (Name, Department)
employees.put("John", "HR");
employees.put("Eric", "IT");
employees.put("Joe", "LEGAL");
employees.forEach((name, department) -> {
System.out.format("(Name): %s, (Department): %s %n", name, department);
});
}
}
forEach Loop code output:
key (Name): Joe, value (Department): LEGAL
key (Name): Eric, value (Department): IT
key (Name): John, value (Department): HR
Iterate Hashmap key and values using Stream Api
package javaapplication;
import java.util.Map;
import java.util.HashMap;
public class JavaApplication {
public static void main(String[] args) {
// HashMap object with list of employees with their name and department
Map<String, String> employees = new HashMap<>();
// keys and values (Name, Department)
employees.put("John", "HR");
employees.put("Eric", "IT");
employees.put("Joe", "LEGAL");
employees.entrySet().stream().forEach(emp -> {
System.out.format("key (Name): %s, value (Department): %s %n", emp.getKey(), emp.getValue());
});
}
}
Stream Loop code output:
key (Name): Joe, value (Department): LEGAL
key (Name): Eric, value (Department): IT
key (Name): John, value (Department): HR