Difference Between HashMap And ConcurrentHashMap in Java

ConcurrentHashMap in Java is a thread safe Map implementation which provides another alternative to be used in a multithreaded environment apart from HashTable or explicitly synchronizing HashMap.
Where as HashMap in Java is not synchronized so not thread safe but provides better performance. In this post we’ll see the difference between HashMap and ConcurrentHashMap in Java.

HashMap Vs ConcurrentHashMap in Java

1- Thread safety:

One of the main difference between HashMap and ConcurrentHashMap is that ConcurrentHashMap is thread safe and can be used in concurrent environment.

HashMap is not thread safe and sharing HashMap’s instance in a multi-threaded environment will give unpredictable results.

2- How synchronization is done:

HashMap can be synchronized externally by using Collections.synchronizedMap() method which returns a synchronized Map backed by the specified map. Note that synchronizing a HashMap this way synchronizes all the methods of the HashMap on a single lock.

ConcurrentHashMap is already thread safe so no external synchronization needed. ConcurrentHashMap provides better concurrency by locking only a portion of the Map. Map is divided into partitions based on the passed concurrency level. By default concurrency level is 16. That means 16 threads can modify ConcurrentHashMap if all those threads are accessing different partitions of the Map.
ConcurrentHashMap allows retrieval operations (for example get) to overlap with update operations (for example put and remove) as retrieval operations generally do not block. This helps in improving the performance of the ConcurrentHashMap.

3- Iterator fail-fast or fail-safe

The iterators returned by HashMap’s “collection view methods” are fail-fast. If the 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.

Iterator returned by ConcurrentHashMap’s “collection view methods” are fail-safe and does not throw ConcurrentModificationException if the map is structurally modified at any time after the iterator is created.

Here is an example showing the iteration using an iterator with both HashMap and ConcurrentHashMap.

Output

As you can see ConcurrentModificationException exception is thrown as you are trying to structurally modify the HashMap while it is iterated using an iterator.

Where as in case of ConcurrentHashMap-

Output

As you can see while iterating the ConcurrentHashMap a new element is added to it, that does not result in ConcurrentModificationException being thrown.

4- Allowing null

HashMap permits null values and the null key.

ConcurrentHashMap does not allow null to be used as a key or value.

5- Performance

HashMap is faster as it is not synchronized. If HashMap instance has to be shared among multiple threads and it has to be synchronized externally then HashMap is slower in comparison to ConcurrentHashMap as the whole HashMap is synchronized on a single lock in such scenario where as ConcurrentHashMap uses separate locks for separate buckets thus providing much better concurrency level.

That’s all for the topic Difference Between HashMap And ConcurrentHashMap in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • Difference Between ArrayList And CopyOnArrayList in Java
  • HashSet Vs LinkedHashSet Vs TreeSet in Java
  • Static Methods in Java Interface
  • final Vs finally Vs finalize in Java
  • Java Program to Find First Non-Repeated Character in The Given String
  • Sequence File Format in Hadoop
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.