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

What do you understand by the term wrapper classes?

542


What are the benefits of immutable objects?

534


What is meant by nested loop?

541


What are the three types of design patterns?

510


what is synchronization? : Java thread

563






What do you mean by singleton class in java?

514


Can a final variable be manipulated in java?

532


Where are register variables stored?

591


Can constructor be synchronized?

543


What is a data structure java?

500


What is a parameter in simple terms?

557


What is difference between string and stringbuffer?

490


Which is bigger float or double?

514


Explain what are final variable in java?

574


What is ‘is-a ‘ relationship in java?

588