If you have a map and wants to convert into a different map, say mapping the value to something different, it would be sensible to utilise Java 8 Lambda, right?
Map newMap = oldMap.entrySet()
.stream()
.collect(Map.Entry::getKey, entry -> someMapperFunc(entry.getValue()));
Unfortunately the lambda does not ALWAYS Work, depends on whether there is any NULL value in the original map.
As documented in JavaDoc, the intention of the `toMap()` is to "Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements." So why does it not want to accept null value?
You'll have to be thorough and patient enough to read all the three overloaded versions of `toMap()` to be able to figure out that, `Collector.toMap()` relies on `Map.merge()`, which is designed to throw NPE when a NULL value is passed in.
Who would expect that? Idiot library implementation ...
Refer to:
1. [StackOverflow - NPE in Collectors.toMap](https://stackoverflow.com/questions/24630963/java-8-nullpointerexception-in-collectors-tomap)
2. [Bug reported in OpenJDK](https://bugs.openjdk.java.net/browse/JDK-8148463)
评论