File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Difference between a HashMap and HashTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between a HashMap and HashTable" Watch "Difference between a HashMap and HashTable" New topic

Difference between a HashMap and HashTable

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 751
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?

SCJP 1.5
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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.

There is no emoticon for what I am feeling!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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.

Edwin Dalorzo
I agree. Here's the link:
subject: Difference between a HashMap and HashTable
It's not a secret anymore!