| 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
|
 |
 |
|
|
subject: Why Hashtable does not accept null as keys and values ?
|
|
|