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


Please Help Members By Posting Answers For Below Questions

Which of the following classes will have more memory allocated?

805


What is unicode datatype?

735


Does anyone still use java?

868


Are global variables initialized to zero?

732


What is sortedmap in java?

795


What causes memory leak in java?

718


What is an image buffer?

746


What is boolean query?

769


How do you compare two objects?

734


What is bytecode in java ?

872


What is the difference between inner class and nested class?

786


What is currentthread()?

791


When can an object reference be cast to an interface reference in java programming?

795


Can we call virtual funciton in a constructor ?

1975


What is meant by structural programming?

816