*
The moose likes Beginning Java and the fly likes Why HashTable does not allow null values..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why HashTable does not allow null values..?" Watch "Why HashTable does not allow null values..?" New topic
Author

Why HashTable does not allow null values..?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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"]


Creativity is nothing but Breaking Rules
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

"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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Sorry for the misplacing of numm instead of null
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Oct 13, 2005
Posts: 39076
    
  23
Agree with Rob; there are instances where you want to permit nulls and instances when you want to prohibit them.
 
GeeCON Prague 2014
 
subject: Why HashTable does not allow null values..?