So I'm trying to use the Java 8 streams Collectors.toMap to add elements to a newly created Map.
The Map to be created may contain null values for some of its keys. This is perfectly acceptable for a HashMap, and when I use a stream forEach to add member it works as expected:
final Map<String, Method> rMap = new HashMap<> ( );
msgRouteProps.entrySet ( )
.stream ( )
.forEach (entry -> rMap.put (entry.getKey ( ),
ReflectionUtil.getNamedMethod (processor, entry.getValue ( ))));
The msgRouteProps is a Map where the keys and values are all non-null. Note that the ReflectionUtil.getNamedMethod () call may return a null, which I want to be put into the resulting Map.
So I wanted to explore how to do the same thing with the Collectors.toMap() method, so the stream operation could return the newly created map, without me having to create it directly.
So I tried the following:
Map<String, Method> rMap =
msgRouteProps.entrySet ( )
.stream ( )
.collect (Collectors.toMap (Map.Entry::getKey,
(e) -> ReflectionUtil.getNamedMethod (processor, e.getValue ( ))));
It seems to me that this should work the same way, but unfortunately, it fails with a null pointer exception. It appears that the Collectors.toMap method calls the HashMap's merge() method which doesn't allow null values to be passed. What seems odd to me is that I don't really think that merge should be getting called at all in this case, since the keys in the original map are all unique. Given that, I would assume that the values could simply be added to the new map via calling the map's put() method, which will allow null values.
Is there something that I'm doing wrong here, or is there a work-around for this? Or is this simply the expected behavior when using Collectors.toMap?