I have a Map (a multi-map, of sorts) with the following signature:
Map<String, Set<String>> motherChildIndex;
If the key doesn't exist in the map, I would prefer that it return an empty set (as opposed to null), so I stumbled upon the getOrDefault
method, which should allow me to "ignore" nulls, effectively.
Here is my relevant code:
motherChildIndex
.getOrDefault(personId, Collections.emptySet()) //personId is just a string that may or may not be in the map, hence getOrDefault
.stream() //I get the warning here
.map(personsIndex::get) //Not important to reproduce
.forEach(children::add); //Not important to reproduce
On the .stream()
line, I get a warning:
Method invocation 'stream' may produce 'java.lang.NullPointerException'
Why? I thought getOrDefault()
was supposed to never return a null.
I'm using Android Studio as my IDE, if that makes any difference.