Difference between synchronized HashMap and HashTable?
Both HashTable and HashMap implements Map interface but there are some differences between these two. They are:
- Thread Safety (synchronized)
- Null Keys
- Time Complexity
Thread Safety (synchronized)
First and most significant different between Hashtable and HashMap is that, HashMap is not thread-safe (unsynchronized) while Hashtable is a thread-safe (synchronized) collection. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.
Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
You can make the HashMap as thread-safe (synchronized) by calling this code