• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference between a HashMap and HashTable

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic