0

How to get keys and values from Map in Java

Java
Map<String, String> map = new HashMap<>();

// Get keys and values
for (Map.Entry<String, String> entry : map.entrySet()) {
	String k = entry.getKey();
	String v = entry.getValue();
  
	System.out.println("Key: " + k + ", Value: " + v);
}

Java 8

Map<String, String> map = new HashMap<>();

map.forEach((k, v) -> {
	System.out.println("Key: " + k + ", Value: " + v);
});
add comment
Suggest a different solution or add another example