Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why HashTable does not allow null values..?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all

As we know that Hashtable does not allow null values as keys and values,Kindly tell me why they made the implementation like that other than Hashmap which allows null

[Jesper Young: edited topic title, "numm" -> "null"]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"numm value"? Please take the time to proof-read your posts.

Of what utility would they be? Null values would be indistinguishable from missing values except during iteration, and of what use would a null key be?
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the misplacing of numm instead of null
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Null values would be indistinguishable from missing values except during iteration

That's where containsKey comes in.

and of what use would a null key be?

It allows you to store the key without a value. Granted, I never saw the need for that, but still.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the reason behind that why they made to allow null values in hashmap i had gone through the API that while putting they are going to check the null values.

 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the more important question is: why would you NOT want to allow null keys and values? Using null values for defaults (if there is no value for a non-null key, check the value for the null key) isn't uncommon. If you want to disallow them, use Collections.checkedMap - that one forbids null as well.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48980
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Rob; there are instances where you want to permit nulls and instances when you want to prohibit them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic