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 multithreading in java?
What do you mean by an interface in java?
How many days will it take to learn java?
How to implement an arraylist in java?
How do you sort in ascending order in java?
Is hashmap thread safe?
When a lot of changes are required in data, which one should be a preference to be used? String or stringbuffer?
Why do we override tostring method in java?
How do you calculate square roots?
What are the different http methods?
Explain reverse a linked list recursive java solution?
What are different data structures in java?
What is an immutable class? How to create an immutable class?
Why main() method is public, static and void in java ?
What does .equals do in java?