Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it useful to have null values or null keys in hash maps?

Tags:

java

hashmap

Hashtable does not allow null keys or values, while HashMap allows null values and 1 null key.

Questions:

  1. Why is this so?
  2. How is it useful to have such a key and values in HashMap?
like image 878
sab Avatar asked Sep 01 '10 20:09

sab


People also ask

Why do we use null key in HashMap?

Now you must be wondering why HashTable doesn't allow null and HashMap do? The answer is simple. In order to successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method. Since null is not an object, it can't implement these methods.

What happens when we pass null as key in HashMap?

If you pass null as map key, it will go to 0 bucket . All values of null key will go there. That is why it returns same value, cause all keys you are providing are null and are in the same bucket of your HashMap.

Can we use null key in HashMap?

HashMap is similar to HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values.

Can we put null as key in map?

Yes, null is always a valid map key for any type of map key (including primitives, sobjects, and user-defined objects). Do you mean doing something like this? Map<String, String> myMap = new Map<String, String>{}; myMap. put('key1', null);


2 Answers

1. Why is this so?

HashMap is newer than Hashtable and fixes some of its limitations.

I can only guess what the designers were thinking, but here are my guesses:

  • Hashtable calculates a hash for each key by calling hashCode on each key. This would fail if the key were null, so this could be a reason for disallowing nulls as keys.
  • The method Hashtable.get returns null if the key is not present. If null were a valid value it would be ambiguous as to whether null meant that the key was present but had value null, or if the key was absent. Ambiguity is bad, so this could be a reason for disallowing nulls as values.

However it turns out that sometimes you do actually want to store nulls so the restrictions were removed in HashMap. The following warning was also included in the documentation for HashMap.get:

A return value of null does not necessarily indicate that the map contains no mapping for the key; it is also possible that the map explicitly maps the key to null.


2. How is it useful to have such a key and values in HashMap?

It is useful to explicitly store null to distinguish between a key that you know exists but doesn't have an associated value and a key that doesn't exist. An example is a list of registered users and their birthdays. If you ask for a specific user's birthday you want to be able to distinguish between that user not existing and the user existing but they haven't entered their birthday.

I can't think of any (good) reason for wanting to store null as a key, and in general I'd advise against using null as a key, but presumably there is at least one person somewhere that needs that keys that can be null.

like image 97
Mark Byers Avatar answered Oct 08 '22 22:10

Mark Byers


Well, I think Mark Byers answered perfectly, so just a simple example where null values and keys can be useful:

Imagine you have an expensive function that always returns the same result for the same input. A map is a simple way for caching its results. Maybe sometimes the function will return null, but you need to save it anyway, because the execution is expensive. So, null values must be stored. The same applies to null key if it's an accepted input for the function.

like image 42
sinuhepop Avatar answered Oct 08 '22 22:10

sinuhepop