Using for each to iterate through a HashMap
Using for each to iterate through a HashMap
import java.util.HashMap;
import java.util.Map;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Iterate through HashMap with Lambda Expressions in Java 8
import java.util.HashMap;
import java.util.Map;
public class IterateHashMap {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.forEach((key,value) -> System.out.println(key + " = " + value));
}
}