Java: HashMap.getOrDefault returns null value

I’m fairly new to Java coming from a Python and C# background. I don’t know why I am getting a null value for HashMap.getOrDefault() when from my understanding, this method is built in the first place to avoid NullPointer exceptions. The people object is not null, neither is idKey.

Screen Shot

Answer

The getOrDefault() will return the default value when the key is not found, as per the documentation.
If the key is found but the value is null, then null will be returned.

So, it seems that your map actually contains the key but the corresponding value is null.

Leave a Reply

Your email address will not be published. Required fields are marked *