how to print values in hashmap in java code example

Example 1: print elements in map java

// Java 8 - Collection.iterator() + Iterator.forEachRemaining()
        map.keySet().iterator()
                .forEachRemaining(System.out::println);

Example 2: print map in java

Map map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
System.out.println(Arrays.asList(map)); // method 1
System.out.println(Collections.singletonList(map)); // method 2

Example 3: print only values in map

map.forEach((key, value) -> System.out.println(key + " " + value));

Tags:

Misc Example