HashTable vs HashMap in Java

Even though HashTable and HashMap both are data-structure based upon Hashing and implementation of Map Interface, there are many differences among them. We will read about the main difference between them when implemented in Java Programming Language.

Hashtable vs HashMap

FactorsHashMapHashtable
Synchronizationjava.util.HashMap 
methods is not synchronized, not thread-safe and can’t be shared between many threads
java.util.Hashtable
methods is synchronized, thread-safe and can be shared
with many threads
Null Key/ValuesHashMap allows one null key and multiple null ValuesHashtable does not allows any null key or value
PreferenceHashMap is preferred if thread synchronization is
not needed
Since Hashtable is
legacy class ,use ConcurrentHashMap instead if needed synchronization
InstantiateMap<String, List<String>> foo = new HashMap<String, List<String>>();Map<String ,List<String>>
foo = new Hashtable<String,
List<String>>();
Insertion Order Does not maintain Insertion OrderDoes not maintain Insertion Order

Why HashTable doesn’t allow null and HashMap does?

To successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method. Since null is not an object, it can’t implement these methods. HashMap is an advanced version and improvement on the Hashtable. HashMap was created later.

source: HashTable and Hash Map Differences

Maintaining Insertion order in Hashmap

HashMap

HashMap itself doesnot maintain insertion order – but LinkedHashMap does. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

LinkedHashMap

It is the Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

Concurrent Hash Map

ConcurrentHashMap is the implementation of HashMap which is thread-safe. It allows multiple readers to read concurrently without any blocking. This is achieved by partitioning Map into different parts based on concurrency level and locking only a portion of Map during updates.