Can you store null in a HashMap key? If yes, how does it handle it?
Yes, you can store a null key in a HashMap. Here’s how it handles it:
-
Allows One null Key: A HashMap permits only one null key because keys must be unique.
-
Storage Mechanism: The null key is handled specially:
-
It doesn’t calculate a hash code (since null has no hash value).
-
It is always stored in the first bucket (index 0).
-
In short: HashMap supports one null key and stores it in a predefined way.