Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what's the difference between hashtable and hashMap

 
rose deng
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A HashMap also permits a null key
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you should never use Hashtable. If you need a synchronized HashMap then use:
Map syncMap = Collections.synchronizedMap(myMap);
 
John Smith
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just curious; why should Hashtable never be used?
Randy
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic