Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Hashtable doesn't allow null value and HashMap allows only one null as key

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Could you please advice the reason behind why Hashtable doesn't allow null value and HashMap allows only one null as key
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the keys must be unique. Therefore, you can only have one key that is null.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tom Reilly wrote:Because the keys must be unique. Therefore, you can only have one key that is null.

So, why Hashtable doesn't allow null value at all
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So, why Hashtable doesn't allow null value at all

I'll turn it back on you. Why would HashMap allow a null key?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Keys must be unique in Map. HashMap can have maximum one null value

Tom Reilly wrote:

So, why Hashtable doesn't allow null value at all

I'll turn it back on you. Why would HashMap allow a null key?

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the APIs, we can see, the objects which stored in the Hashtable should implement hashCode() and equals() methods.

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.


But, However the 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.

I think, it should be the reason.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are many previous discussions about this in the "Beginning Java" and "Java In General" forums, including this one http://www.coderanch.com/t/409245/java/java/Why-does-HashMap-allow-null
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!