Why Hash table doesn't allow null values as well as null keys?

Showing Answers 1 - 9 of 9 Answers

HashTable:
1.) From the APIs, we can see, the objects which stored in the Hashtable should implement hashCode() and equals() methods.
2.) To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
HashMap:
 HashMap implementation does allow null keys. In the java.util.HashMap implementation, in case the key is null, it uses a surrogate object to act as the key. The surrogate key is a dummy non-null java object. However using a null key in HashMap overwrites the previous null-key : value mapping.

  Was this answer useful?  Yes

Vinay Kumar

  • Nov 23rd, 2015
 

Hashtable is a legacy class. hash table doesnt allow duplicate object as a key and value beacause hashtable implements hashcode () method. Hash code method compute object hashcode number if object is a null as key and value then hashtable doesnt get any hash code of key or value then JVM throws NullPointerException. and if try to store null value in hashtable then Hashtable.get() method return null if key was absent would be ambiguous. in this case we cant store null object as key and value because hashtable implements hashcode() and equals() for calculate object.
This is a drawback of Hash table. Hashmap is newer class which is added JDK 1.2 version. HashMap developed for specially store one null key and many value and it is non-thread safe and non-synchronized what are drawback available in hashtable all feature provide in Hashmap.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions