Points To Remember
- HashTable have the legacy code and were introduced before HashMaps.
- There is nothing in a HashTable that a HashMap cannot do except multithreading.
- ConcurrentHashMap is a replacement for HashTable, it a synchronized HashMap.
Differences : HashMap and HashTable
|They implement Map Interface.||It extends Dictionary(obsolete from jdk 1.7) class and implements Map interface|
|They are a part of Collections API||They are not a part of Collections API|
|They allow a single null key and any number of null values.||They do not allow null values to be added.|
|They are not synchronized or thread-safe||They are synchronized or thread-safe|
|They use Iterator to iterate over objects||They use Enumeration to iterate over objects|
|They are faster and uses less memory||They are comparatively slower.|
Similarities : HashMap and HashTable
- Both of them implements Map interface.
- Both HashMap and Hashtable works on the Principle of Hashing.
- Both HashMap and HashTable do not guarantee that the order of the map will remain constant over time.
I recommend not to use HashTable and use HashMap since HashMap is much faster than the HashTables and it allows you to do everything that a HashTable allows you to do. In case you are in need of using a Map in a multithreaded environment than you should go for ConcurrentHashMap. A ConcurrentHashMap provides all the functions of a HAshMap and it is also thread safe or simple multithreaded.