HashMap, ConcurrentHashMap, HashTable and Collections.synchronizedMap differences

Question 1

 
Differences between HashMap and Hashtable ?
(OR)
What are the differences between a HashMap and a Hashtable in Java?
 

Answer

 
There are several differences between HashMap and Hashtable in Java:

– Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for non-threaded applications, as unsynchronized Objects typically perform better than synchronized ones.

– Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.

– One of HashMap’s subclasses is LinkedHashMap, so in the event that you’d want predictable iteration order (which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap. This wouldn’t be as easy if you were using Hashtable.
 

You may refer more on HashMap here:

HashMap in Java

Question 2

 
What is the difference between ConcurrentHashMap and Hashtable in Java ?
 

Answer

 
ConcurrentHashMap uses multiple buckets to store data. This avoids read locks and greatly improves performance over a HashTable. Both are thread safe, but there are obvious performance wins with ConcurrentHashMap.

When you read from a ConcurrentHashMap using get(), there are no locks, contrary to the HashTable for which all operations are simply synchronized. HashTable was released in old versions of Java whereas ConcurrentHashMap is introduced in JDK 5.
 

Refer more on ConcurrentHashMap in this article :

ConcurrentHashMap in Java

Question 3

 
What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
 

Answer

 
The main difference between these two is that ConcurrentHashMap will lock only portion of the data which are being updated while other portion of data can be accessed by other threads. However, Collections.synchronizedMap() will lock all the data while updating, other threads can only access the data when the lock is released. If there are many update operations and relative small amount of read operations, you should choose ConcurrentHashMap.
 
Also one other difference is that ConcurrentHashMap will not preserve the order of elements in the Map passed in. It is similar to HashMap when storing data. There is no guarantee that the element order is preserved. While Collections.synchronizedMap() will preserve the elements order of the Map passed in. For example, if you pass a TreeMap to ConcurrentHashMap, the elements order in the ConcurrentHashMap may not be the same as the order in the TreeMap, but Collections.synchronizedMap() will preserve the order.
 
Furthermore, ConcurrentHashMap can guarantee that there is no ConcurrentModificationException thrown while one thread is updating the map and another thread is traversing the iterator obtained from the map. However, Collections.synchronizedMap() is not guaranteed on this.

 

Question 4

 
What is the difference between a HashMap, ConcurrentHashMap and a Hashtable in Java?
 

Answer

 

Hashtable:

Hashtable is synchronized, preventing two threads from accessing it at the same time. As of the Java 2 platform v1.2, this class was retrofitted to implement the Map interface, making it a member of the Java Collections Framework.
 

HashMap

If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable.
 

HashMap is not synchronized. If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.)
 
Synchronizing does not prevent ConcurrentModificationException, because they both say: The iterators returned by all of this class’s “collection view methods” are fail-fast: if the [Hashtable/map] is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException.
 
Note that a single thread can cause ConcurrentModificationException by updating the Hashtable/HashMap directly while also iterating it. It doesn’t require multi-threading to violate this rule.
 

ConcurrentHashMap

If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
 
In ConcurrentHashMap, Iterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException.
 
However, iterators are designed to be used by only one thread at a time.
 
So, HashMap is useful for single-threaded access. Hashtable is useful for multi-threaded access, as long as they don’t need to iterate the map. ConcurrentHashMap allows updates and iteration by multiple threads.

 

Here are some more articles that you may like :

 

HashMap Interview Questions

WeakHashMap in Java

Iterating over a map in Java

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags