aspose file tools
The moose likes Java in General and the fly likes Why Hashtable not allowing null key values? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why Hashtable not allowing null key values?" Watch "Why Hashtable not allowing null key values?" New topic
Author

Why Hashtable not allowing null key values?

Cheenath Ajay
Ranch Hand

Joined: Jan 26, 2006
Posts: 32
Any one can help me out for finding an answer for ,

Why Hashtable not allowing null key values?
Why letter 't' is small in HashTable?

Thanks in advance.

Cheenath.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look at the Hashtable JavaDoc. The objects used as keys must implement what method? Can a null implement that?

Re the lower case "t" ... maybe the designer had a hang-over that day. It wasn't the most "common sense" decision. It's generally recommended to use HashMap nowadays and leave Hashtable to historical code.

Keep asking questions!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[Stan]: Look at the Hashtable JavaDoc. The objects used as keys must implement what method? Can a null implement that?

Ummm, also look at the HashMap javadoc. Why can a HashMap handle a null key, while a Hashtable can't? My answer: the HashMap coders simply chose to do a little more work to handle null as a special case. I believe the rationale is that they were trying to make this implementation as "general purpose" as possible. In contrast the Hashtable coders didn't think this was an important feature to include (and I agree) - and since the earliest versions of Hashtable did not allow null, and documented this fact in the API, it's stayed that way since then for backwards compatibility.


"I'm not back." - Bill Harding, Twister
Cheenath Ajay
Ranch Hand

Joined: Jan 26, 2006
Posts: 32
Hi All,

I got a little better answer from my friend for the Query asked, Please reply if there is anything went wrong!!


Why "null" cannot be used as key value in Hashtable.



The reason is quite simple. Hashtable mandates that the objects used as keys

must implement the hascode method and the equals method. The methods so

implemented by the keys are then used internally by Hashtable for storage and

retrieval. Therefore null cannot be used as keys in Hashtable.



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.

Thanks to All,

Cheenath.
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
In HashMap if you want to use your own class as a key, then you should override the equals() and hashCode() method. Also, be aware that HashMap is not thread safe and if you need thread safety use Collections classes' synchronize method. [Hashtable is thread safe but do not use it]. if you are using JDK 1.5, the have a look at java.util.concurrent.* package for more efficient implementation of threadsafe HashMap.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Interesting. In 1.4 doc HashMap doesn't mention hashCode() at all except as a "see also" and because it implements it. Seems like they'd mention the key object ought to implement it in a meaningful way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Hashtable not allowing null key values?
 
Similar Threads
difference between hashmap&hashtable
hashmap with null values and keys
Vector and null
HashMap Vs Hash table
Hashtable allowing null values