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
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.
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.
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