how to print map values code example
Example 1: how to print the map in java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
class MapUtil
{
public static void main (String[] args)
{
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
Iterator<Integer> itr = map.keySet().iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
for (Integer key : map.keySet()) {
System.out.println(key);
}
map.keySet().iterator()
.forEachRemaining(System.out::println);
map.keySet().stream()
.forEach(System.out::println);
Stream.of(map.keySet().toArray())
.forEach(System.out::println);
System.out.println(map.keySet().toString());
Stream.of(map.keySet().toString())
.forEach(System.out::println);
}
}
Example 2: print elements in map java
map.keySet().iterator()
.forEachRemaining(System.out::println);