Java8: HashMap to HashMap using Stream / Map-Reduce / Collector

I know how to “transform” a simple Java List from Y -> Z, i.e.: List<String> x; List<Integer> y = x.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); Now I’d like to do basically the same with a Map, i.e.: INPUT: { “key1” -> “41”, // “41” and “42” “key2” -> “42” // are Strings } OUTPUT: { “key1” … Read more

NullPointerException in Collectors.toMap with null entry values

Collectors.toMap throws a NullPointerException if one of the values is null. I don’t understand this behaviour, maps can contain null pointers as value without any problems. Is there a good reason why values cannot be null for Collectors.toMap? Also, is there a nice Java 8 way of fixing this, or should I revert to plain … Read more