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
what is thread? : Java thread
Can you declare a private method as static?
What are java annotations?
Can we restart a thread already started in java?
Is special character in java?
What is a bubble sort in java?
According to java operator precedence, which operator is considered to be with highest precedence?
What is the purpose of the system class in java programming?
What is a class reference?
What is difference between c++ and java ?
What is java util?
What happens if an exception is not handled in a program?
What is string [] args?
Describe the term diamond problem.
How to convert string to byte array and vice versa?