With Java 9, new factory methods have been introduced for the List
, Set
and Map
interfaces. These methods allow quickly instantiating a Ma
Allowing nulls in maps has been an error. We can see it now, but I guess it wasn't clear when HashMap
was introduced. NullpointerException
is the most common bug seen in production code.
I would say that the JDK goes in the direction of helping developers fight the NPE plague. Some examples:
Optional
Collectors.toMap(keyMapper, valueMapper)
doesn't allow neither the keyMapper
nor the valueMapper
function to return a null
valueStream.findFirst()
and Stream.findAny()
throw NPE if the value found is null
So, I think that disallowing null
in the new JDK9 immutable collections (and maps) goes in the same direction.