This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why Hashtable does not accept null as keys and values ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Why Hashtable does not accept null as keys and values ?" Watch "Why Hashtable does not accept null as keys and values ?" New topic
Author

Why Hashtable does not accept null as keys and values ?

Ishwar Paliwal
Greenhorn

Joined: Sep 26, 2007
Posts: 2
Please someone tell me reason about why Hashtable does not accept a null as keys and values while HashMap accept. Whereas both are based on the same data structute(hashtable).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

It's a design decision.

Hashtable was created for Java 1.0, and it's API shows it. It's elements() method returns an Enumeration over the values, and the keys() method returns an Enumeration over the keys. Both shouldn't be used; use values() and keySet() instead.

When in Java 1.2 the Collections framework was introduced, the developers put a lot more though into it, and it shows. No more Enumerations but Collection/Set, for instance. Short method names (List has get() instead of Vector's elementAt()). Hashtable has been retrofitted, but it's still a poor choice most of the time.
HashMap was created for the Collections framework, and the developers didn't see a reason to disallow null values.

Basically, if you don't need synchronization, use HashMap instead. If you do need synchronization, you could use Hashtable, or just use a HashMap wrapped by Collections.synchronizedMap() method.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Rob Prime:
Basically, if you don't need synchronization, use HashMap instead. If you do need synchronization, you could use Hashtable, or just use a HashMap wrapped by Collections.synchronizedMap() method.


I would go further and say never use Hashtable in new code for Java 1.2+.

Internal synchronisation in a Map is very rarely useful. When you really do need it, use Collections.synchronizedMap().


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Hashtable does not accept null as keys and values ?
 
Similar Threads
Vector and null
Why hashMap Allwos one Null Keys and Multiple Null Values ? While hashtable doesnot !
HashSet will allow null values?
HashTable and HashMap
Why HashTable does not allow null values..?