Overview
In this tutorial, we show you how to print out all keys and values in Map (HashMap) in Java 10.
Reference Tutorials
Print all keys and values in HashMap using forEach + lambda expression
package com.jackrutorial;
import java.util.HashMap;
public class PrintingHashmapExample1 {
public static void main (String[] args) {
var map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring MVC");
map.put(3, "AngularJS");
//Using forEach + lambda expression
System.out.println("Using forEach + lambda expression");
map.forEach((k,v) -> System.out.println(k + " - " + v));
//Using for each loop
System.out.println("Using for each loop");
for (var key : map.keySet()) {
System.out.println("key: " + key);
}
for (var value : map.values()) {
System.out.println("value: " + value);
}
}
}
Output
Using forEach + lambda expression
1 - Java
2 - Spring MVC
3 - AngularJS
Using for each loop
key: 1
key: 2
key: 3
value: Java
value: Spring MVC
value: AngularJS
Print all keys and values in HashMap using Collection.iterator() & Iterator.forEachRemaining()
package com.jackrutorial;
import java.util.HashMap;
public class PrintingHashmapExample2 {
public static void main (String[] args) {
var map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring MVC");
map.put(3, "AngularJS");
//Using Collection.iterator() & Iterator.forEachRemaining()
System.out.println("Using Collection.iterator() & Iterator.forEachRemaining()");
map.keySet().iterator().forEachRemaining(k -> {
System.out.println("Key: " + k);
});
map.values().iterator().forEachRemaining(v -> {
System.out.println("Value: " + v);
});
}
}
Output
Using Collection.iterator() & Iterator.forEachRemaining()
Key: 1
Key: 2
Key: 3
Value: Java
Value: Spring MVC
Value: AngularJS
Print all keys and values in HashMap using Collection.stream() & Stream.forEach()
package com.jackrutorial;
import java.util.HashMap;
public class PrintingHashmapExample3 {
public static void main(String[] args) {
var map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring MVC");
map.put(3, "AngularJS");
// Using Collection.stream() & Stream.forEach()
System.out.println("Using Collection.stream() & Stream.forEach()");
map.keySet().stream().forEach(k -> {
System.out.println("Key: " + k);
});
map.values().stream().forEach(k -> {
System.out.println("Key: " + k);
});
}
}
Output
Using Collection.stream() & Stream.forEach()
Key: 1
Key: 2
Key: 3
Key: Java
Key: Spring MVC
Key: AngularJS
Print all keys and values in HashMap using Stream.of() & Stream.forEach()
package com.jackrutorial;
import java.util.HashMap;
import java.util.stream.Stream;
public class PrintingHashmapExample4 {
public static void main(String[] args) {
var map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring MVC");
map.put(3, "AngularJS");
// Using Stream.of() & Stream.forEach()
System.out.println("Using Stream.of() & Stream.forEach()");
Stream.of(map.keySet().toString()).forEach(k -> {
System.out.println("Key: " + k);
});
Stream.of(map.values().toString()).forEach(v -> {
System.out.println("Value: " + v);
});
}
}
Output
Using Stream.of() & Stream.forEach()
Key: [1, 2, 3]
Value: [Java, Spring MVC, AngularJS]
Print all keys and values in HashMap using Iterator
package com.jackrutorial;
import java.util.HashMap;
public class PrintingHashmapExample5 {
public static void main(String[] args) {
var map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring MVC");
map.put(3, "AngularJS");
// Using Iterator
System.out.println("Using Iterator");
var keyItr = map.keySet().iterator();
while (keyItr.hasNext()) {
System.out.println("Key: " + keyItr.next());
}
var vItr = map.values().iterator();
while (vItr.hasNext()) {
System.out.println("Value: " + vItr.next());
}
}
}
Output
Using Iterator
Key: 1
Key: 2
Key: 3
Value: Java
Value: Spring MVC
Value: AngularJS