This is not a question about the differences between I understand that a Hashtable
and HashMap
.Hashtable
object cannot accept null
values for either key or value entries, that it is synchronized collection, and that it uses slightly less memory than a HashMap
.
I'm wondering about the scenarios where it would be more appropriate to use a Hashtable
instead of a HashMap
.
HashMap is non-syncronized and is not thread safe while HashTable is thread safe and is synchronized. HashMap allows one null key and values can be null whereas HashTable doesn't allow null key or value. HashMap is faster than HashTable. HashMap iterator is fail-safe where HashTable iterator is not fail-safe.
The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. It is similar to HashMap, but is synchronized.
HashMap is not synchronized, therefore it's faster and uses less memory than Hashtable. Generally, unsynchronized objects are faster than synchronized ones in a single threaded application.
Using HashMap makes sense only when unique keys are available for the data we want to store. We should use it when searching for items based on a key and quick access time is an important requirement. We should avoid using HashMap when it is important to maintain the same order of items in a collection.
This is not a question about the differences between
Hashtable
andHashMap
Well it is really...
I'm wondering about the scenarios where it would be more appropriate to use a
Hashtable
instead of aHashMap
.
Precisely when you want the differences between the two:
Collections.synchronizedMap
over a HashMap
Hashtable
(relatively rare, fortunately)I can't remember the last time I was in that situation, personally - I would say it's vanishingly rare to be appropriate to use Hashtable
in modern Java code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With