-1

We can do null insertion in Hash Map for both Key and value but in case of Hash table we are not able to do null insertion?

  • Please refer https://stackoverflow.com/questions/11981852/why-hashtable-does-not-allow-null-keys-or-values this link. – GauravRai1512 Sep 17 '19 at 06:12
  • yes, but is that the question? its very well documented, first lines of documentation of both classes... (and use of `HashTable` is not recommended, there are better classes (at least since Java 5)) – user85421 Sep 17 '19 at 06:14

1 Answers1

0

To successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method. Since null is not an object, it can’t implement these methods. HashMap is an advanced version and improvement on the Hashtable. HashMap was created later.

Null key in hashmap is always stored on 1st position. so when ever you request a null key in hashmap its go for first position. retrive the key and value. than return it.

Only 1 null key is allowed in hashmap as it can not contain duplicate keys.

ConcurrentHashMap is a newer class but also has the restriction of not allowing null keys or values. They add this restriction for performance reasons since it's a lot of extra work to support null keys and values but probably not useful the majority of the time.

SSP
  • 2,650
  • 5
  • 31
  • 49