Why does Hashtable not take a null
key?
Also why does HashMa开发者_JAVA技巧p allow null
keys?
What is the purpose of making these two classes Key behaviour so different?
From the Hashtable
JavaDoc:
To successfully store and retrieve objects from a hashtable, the objects used
as keys must implement the hashCode method and the equals method.
In a nutshell, since null
isn't an object, you can't call .equals()
or .hashCode()
on it, so the Hashtable
can't compute a hash to use it as a key.
HashMap
is newer, and has more advanced capabilities, which are basically just an improvement on the Hashtable
functionality. As such, when HashMap
was created, it was specifically designed to handle null
values as keys and handles them as a special case.
Specifically, the use of null
as a key is handled like this when issuing a .get(key)
:
(key==null ? k==null : key.equals(k))
It is just an implementation detail.
Hashtable
is the older class, and its use is generally discouraged. Perhaps they saw the need for a null key, and more importantly - null values, and added it in the HashMap
implementation.
Hashtable predates the collections framework, and was part of JDK 1.0. At that time null keys were probably considered not useful or not essential, and were thus forbidden. You might see it as a design error, just as the choice of the name Hashtable
rather than HashTable
.
Then, several years later, came the collections framework, and Hashtable was slightly modified to fit into the framework. But the behavior on null keys was not changed to keep backward compatibility.
Hashtable should be deprecated, IMHO.
I will let you know how the hashmap stores the objects internally:
HashMap stores the values through put(key,value) and gets the values thorugh get(key)
. The process follows the concept of Hashing.
When we say put(key,value)
- Internally hashCode()
for the key is calculated and being taken as the input for hashfunction()
to find the bucket location for storing.
In case of Collision - while calculating the hashcode()
there may be a possibility the key is different but the hashcode()
is same, at that time after finding out the bucket location the storing is done in the linked list. Please Note - While storing as the Map.Entry both the key-value is stored.
When Retrieve of the value through key is done then if during the collision where the key's hashcode() may be same then the value is retrived though equals()
function to find out the desired key's value.
Regards, Anand
Apart from all the details given in other answers, this is how hashmap allow NULL Keys. If you look at the method putForNullKey()
in Hashmap (JDK 5) , it reserves the index "0" for the null key. All values for the null key are kept inside the "0" index of the array.
There is nothing special about storing NULL value as all the put and lookup operations work based on the Key object.
In hashtable, Java does not have these mechanisms and hence hashtable does not support NULL key or values.
They're two separate classes for two different things. Also, HashTable is synchronized. HashTable also came before HashMap, so naturally it would be less advanced. It probably didn't make sense to generate a null hashcode in early Java.
精彩评论