Learnitweb

Difference between HashTable and HashMap in Java

Both HashMap and HashTable map keys to values. HashTable was introduced in JDK 1.0. As of JDK 1.2, this class was retrofitted to implement the Map interface. Following are the differences between HashTable and HashMap:

HashMapHashTable
HashMap extends java.util.AbstractMapHashTable extends java.util.Dictionary
HashMap allows only one null key and any number of null values.HashTable does not allow null key and null value.
The iterators returned by all of this class’s are fail-fast. If the map is structurally modified after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException.The Enumerations returned by HashTable’s keys and elements methods are not fail-fast.
HashMap is non-synchronized. HashMap can not be shared between multiple threads without proper synchronization.HashTable is synchronized, which means HashTable is thread-safe and can be shared between multiple threads.
Since HashMap is not synchronized like HashTable, HashMap is faster than HashTable.Because of synchronization HashTable is much slower than HashMap.
We can make the HashMap as synchronized by calling this code:
Map m = Collections.synchronizedMap(hashMap);
HashTable is internally synchronized and can’t be unsynchronized.