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

How does java pattern compile work?

746


Can we clone singleton object?

811


Differentiate between a constructor and a method? Can we mark constructors final?

951


What is a superclass?

1116


Why collection is called framework in java?

728


Is it compulsory for a try block to be followed by a catch block in java for exception handling?

784


What is java abstraction with example?

731


What does  mean in regex?

773


Is string passed by reference in java?

764


What does java edition mean?

760


what is the significance of listiterator in java?

809


What are the pillars of java?

679


What are the kinds of polymorphism?

830


What do you mean by an interface in java?

767


Is java ee a framework?

730