Forums Register Login

Under what scenario entry returned by map.entrySet is NULL ?

+Pie Number of slices to send: Send
I came across a code snippet which iterates over a map using its entry set and performs some action only if entry != null

As far as I know even if we don't enter anything in map this method returns an empty set and not a null set.
Even if I put {null,null} then the entry will be [null,null] employing an instance of set with these elements.



I have below question:
1. Under what scenario an entry in HashMap will it be NULL?
2. Is the check even valid ?
1
+Pie Number of slices to send: Send
This never happens; the person who wrote that code was mistaken, or overly concerned about NullPointerExceptions.

The null check is redundant and can be removed without any change in functionality.
+Pie Number of slices to send: Send
It's possible in a HashMap for the key or value to be null. So doing this may make sense in some circumstances:

But as Jesper says, the Entry itself will never be null.
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3123 times.
Similar Threads
swap keys and duplicate values in hashmap
Generic Collections
Generics and Colletions
Why are Generics necessary here?
HELP - Stuck on a TreeMap key problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:35:32.