This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between a HashMap and HashTable

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are they comparable? I'm a little confused with the difference of HashMap and a Hashtable since I think... Both implement the Map interface right?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Hashtable was retrofitted to implement Map, so there is little to distinguish them, apart from Hashtable's methods all beign synchronized (this is like the difference between ArrayList and Vector). And this is not really useful -- if you want a proper threadsafe map, consider ConcurrentHashMap first.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hashtable is a legacy class, it existed before the JCF (Java Collections Framework) as well as Vector and Stack.

The main difference with the new HashMap class is that Hashtable is already synchronized, while HashMap is not. That means that you will have to pay a performance price for using Hashtable.

Hashtable has been retrofitted to implement the main JFC Interfaces and AbstractClasses like Map and AbstractMap simply for us to be able to use it as another Collection class.

But Hashtable class originally implemented Dictionary which is an interface similar to Map and it used Enumeration which is an approach similar to Iterator.

The use of Hashtable is discouraged nowadays, and if you need to syncronize a Map you can use Map myMap = Collections.synchronizedMap(anotherMap)

I hope this helps.

Regards,
Edwin Dalorzo
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic