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

Why Hashtable not allowing null key values?  RSS feed

 
Cheenath Ajay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
Cheenath Ajay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!