why HashTable not allow null key and value
Answer Posted / eknath
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))
| Is This Answer Correct ? | 11 Yes | 1 No |
Post New Answer View All Answers
Can we access instance variables within static methods ?
Is main an identifier?
Explain tree set and its features?
How do you sing an Applet ?
How do you compare characters in java?
How do you sort data in java?
What is string variable?
What is the difference between length and size in java?
What is java util?
What are dot operator queries?
Give an example of use of pointers in java class.
Does substring start with 0?
What are the drawbacks of singleton class?
What is integer parseint?
Which package has light weight components in java programming?