| Author |
What is the difference between Hashtable and HashMap classes?
|
Alexey Korneychuk
Greenhorn
Joined: Sep 06, 2005
Posts: 18
|
|
Hello, ranches! Why does run-time exception occur? What is the difference between Hashtable and HashMap classes? public class J { public static void main(String[] args) throws Exception { HashMap hm = new HashMap(); hm.put(null, null); System.out.println(hm); Thread.sleep(1000); Hashtable ht = new Hashtable(); ht.put(null, null); System.out.println(ht); } } Result: {null=null} java.lang.NullPointerException at java.util.Hashtable.put(Hashtable.java:397) at fgff.Jhsdhdjshdjhsdjhjhjsdhjs.main(J.java:28) Exception in thread "main" Alexey
|
 |
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
|
|
These are the main differences between HashMap & Hashtable: Hashtable does not accept null key / value. Hashtable is synchronized but hash map is not. [ October 14, 2005: Message edited by: Srinivasa Raghavan ]
|
Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
|
 |
Ashwin Kumar
Greenhorn
Joined: Oct 13, 2005
Posts: 27
|
|
That is correct. Also note that only one NULL value is allowed as a key in HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it can have multiple NULL values.
|
SCJP 1.4<br />SCBCD 5.0<br />SCWCD 1.4 (Preparing)
|
 |
Akshay Kiran
Ranch Hand
Joined: Aug 18, 2005
Posts: 220
|
|
Originally posted by AshwinC Kumar: That is correct. Also note that only one NULL value is allowed as a key in HashMap. HashMap does not allow multiple keys to be NULL. Nevertheless, it can have multiple NULL values.
that is however obvious because the keys are implemented in a "Set" and a Set doesn't allow duplicates.
|
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
|
 |
 |
|
|
subject: What is the difference between Hashtable and HashMap classes?
|
|
|