File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Android Security Essentials Live Lessons this week in the Android 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
Similar Threads
HashTable and HashMap
Hashset, Hashmap, Hashtable
HashMap confused