Java Dailly Tip: What are the differences between HashMap and HashTable

Java Dailly Tip: What are the differences between HashMap and HashTable


Hello. During one of the recruitments, I was asked what are the differences between HashMap and HashTable in Java? Which solution is more efficient if we have an application without threads?


This is one of the most frequently asked questions in job interviews. It is used to check whether the candidate understands how to use the collection classes correctly and is aware of the available alternatives. Here are the main differences:

  1. The HashMap class is equivalent to the HashTable class except that it is out of sync and is nullable. HashMap allows you to use null as both key and value, while HashTable doesn't allow null at all.
  2. The HashMap is not syncing and the HashTable is.
  3. The iterator in HashMap is crash-tolerant, while the enumerator for Hashtable is not, and throws a ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except the iterator's own remove () method. However, this is not a guaranteed behavior and will be performed by JVM with best efforts.

Synchronized means that only one thread can modify the hash table at a time. Essentially, this means that each thread will have to obtain the lock on the object before performing an update on the Hashtable table, while others will wait for the lock to be released.

Fail safe is important in the context of iterators. If the iterator was created on the collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception will be thrown. However, it is possible that other threads will call the set method because it does not modify the collection "structurally". However, if the collection was structured before the assembly was called, an IllegalArgumentException exception will be thrown.>/p>

A structural modification is the removal or insertion of an element that could effectively change the structure of the map. HashMap can be synchronized via

M = Collections.synchronizeMap (hashMap);

The map provides collection views instead of directly iterating through enumeration objects. Collection views greatly enhance the clarity of the interface, as discussed later in this section. The map can iterate through keys, values, or key-value pairs; Hashtable does not provide a third option. The map provides a safe way to delete entries during iteration; Hashtable no. Finally, Map fixes a minor flaw in the Hashtable interface. Hashtable has a method named includes that returns true if Hashtable contains the provided value. Given its name, this method can be expected to return true if Hashtable contains the given key, as the key is the primary mechanism for accessing Hashtable. The Map interface eliminates this source of confusion by renaming the ContainsValue method. It also improves the consistency of the —cointainsValue parallels containsKey interface.

Share this Post