This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what's the difference between hashtable and hashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what Watch "what New topic

what's the difference between hashtable and hashMap

rose deng
Ranch Hand

Joined: Dec 29, 2000
Posts: 78
Could anybody tell me what's the difference between hashtable and hashMap, when should i use hashMap, not hashtable.
Thanks a lot.
John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
Hashtable is synchronized and HashMap is not. I believe that is the only difference. Use Hashtable if you need thread-safety. Otherwise, use HashMap since it should be somewhat faster since there's no synchronization.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
A HashMap also permits a null key

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Actually you should never use Hashtable. If you need a synchronized HashMap then use:
Map syncMap = Collections.synchronizedMap(myMap);

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John Smith
Ranch Hand

Joined: Jul 20, 2001
Posts: 84
Just curious; why should Hashtable never be used?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Hashtable, Vector, and Stack are old classes that predate the 1.2 Collections framework.
I guess they haven't been formally deprecated, but Sun recommends that new code use HashMap and ArrayList instead.
And generally, once you create a HashMap, you should store it in a Map variable, and use Map rather than HashMap arguments to methods. This way, you could someday substitute a TreeMap or some other Map easily without changing much code.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
I agree. Here's the link:
subject: what's the difference between hashtable and hashMap
It's not a secret anymore!