• 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

Hashtable vs. HashMap

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All.
It is a general impression that when thread-safe behaviour is not required, it is better to use a HashMap than a Hashtable.
I was wondering why... I wrote a small timer class & have been testing with both of these data structures.
When the number of entries < 20000, the Hashtable takes about 100ms longer to get populated as compared to the hashMap.
When the number of entries > 20000, the HashMap takes about 100ms longer to load than the hashTable.
Can someone explain this & also suggest any other reasons as to why we should use HashMap as opposed to hashTables when no threading is there in the application.
thanks in advance
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as a rule i always think its better to use the new Collection classes which include the HashMap.
they are the new ones and are meant to be used.
also the synchornization thing is misleading and u can get a lot of problems if you rely too much on that ability of hashtable. you still need to take care when accessing or iterating over a hashtable.
so as a rule i would always use a HashMap unless you need a backward compatibility like with Enumaration or some method that accepts only hashtables.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I definitely agree with Roy here. You can get a "thread-safe" view of HashMap using Collections.synchronizedMap() if you really want one - but don't let that be an excuse for not thinking about thread safety carefully after that. You may well find it's necessary to add synchronized() blocks to further protect your data.
One other thing I prefer about the newer collections framework classes - their names are better. "Hashtable" always annoys me because you have to remember not to capitalize the 't', contrary to normal Java practice. And for other classes - why use Enumeration's hasMoreElements() and nextElement() when you can just write hasNext() and next() with an Iterator? Enumeration, Vector, and Hashmap are really just there for backward compatibility - there's rarely a good reason to use them nowadays, unless you're stuck with an existing class or method that uses them.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Is there anyway I can learn the differance between a HashMap and a HashTable.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Himanshu Jhamb:
It is a general impression that when thread-safe behaviour is not required, it is better to use a HashMap than a Hashtable.

I would contend that you are much more likely to write threadsafe code using a HashMap than using a Hashtable. Why? Because the "thread-safety" of Hashtable guarantees that multithreaded access will not mess up your Hashtable. It does by no means guarantee that it won't mess up your application. For example, if you use the Hashtable to realise some form of cache you will typically have logic like "check if the map contains the object; if not, instantiate it and put it in the map". This entire operation needs to be atomic; the fact that in a Hashtable contains and put themselves are atomic buys you absolutely nothing. It only lures you into a false sense of security.
The only significant differences between Hashtable and HashMap are (1) Hashtable is synchronized (I don't like the word "threadsafe" for obvious reasons), with negative implications for performance and the thread safety of your code, and (2) Hashtable carries around both its old API and the Map API, making it rather confusing and ambiguous to work with. Avoid Hashtable and Vector whenever you can, IMHO.
- Peter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic