tags:

views:

99

answers:

2

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?
+7  A: 

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.

Mark Byers
Personally, I don't think `null` values make any sense. They force you to query map twice. Instead of single `map.get()`, you have to use `map.get( )` and `map.containsKey()` to ascertain that key is known to the system. No wonder `null` values are not permitted in many new specializations of `Map` interface.
Alexander Pogrebnyak
Say you were calculating pizza prices. A map could be used to map between topping and additional cost; you could map null to zero and have simpler logic than if you were to special case the toppingless pizza. It's certainly not a *great* example, but I think it suggests what a valid reason might look like.
Carl Manaster
Nice explanation on the validity of null values.
Steve Kuo
+1  A: 

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.

Sinuhe