Wednesday, August 24, 2022

Difference Between HashMap And ConcurrentHashMap in Java

In this post we'll see the differences between ConcurrentHashMap and HashMap in Java which is also a good Java interview question.

ConcurrentHashMap was added in Java 5 as an alternative to HashTable to improve the performance of the (key, value) pair kind of data structure while still keeping it thread safe. On the other hand HashMap in Java is not synchronized so provides better performance but it is not thread safe.

HashMap Vs ConcurrentHashMap in Java

  1. First and foremost difference between HashMap and ConcurrentHashMap in Java is of course thread safety. ConcurrentHashMap is thread safe and fit for use in a multi-threaded environment whereas HashMap is not thread safe.
  2. Second difference is about how these data structures synchronize. HashMap can be synchronized using the Collections.synchronizedMap() method but that synchronizes all the methods of the HashMap on a common lock and effectively reduces it to a data structure where one thread can enter at a time.

    In ConcurrentHashMap synchronization is done a little differently. Rather than locking every method on a common lock, ConcurrentHashMap uses separate lock for separate buckets thus locking only a portion of the Map.

    By default there are 16 buckets and also separate locks for separate buckets. So the default concurrency level is 16. That means theoretically any given time 16 threads can access ConcurrentHashMap if they all are going to separate buckets.

  3. In ConcurrentHashMap performance is further improved by providing read access concurrently without any blocking. Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove).
  4. HashMap allows one null as key but ConcurrentHashMap doesn't allow null as key.

  5. Performace wise HashMap is better as there is no synchronization.

    In case HashMap has to be used in a multi-threaded environment and there is a need to use Collections.SynchronizedMap() method then ConcurrentHashMap() is a better choice as ConcurrentHashMap still gives a chance to more than one thread to access map thus improving performance.

  6. Iterator provided by ConcurrentHashMap is fail-safe which means it will not throw ConcurrentModificationException if the underlying structure is changed during iteration.

    Iterator provided by HashMap is fail-fast as it throws a ConcurrentModificationException if the underlying collection is structurally modified at any time after the iterator is created.

That's all for this topic Difference Between HashMap And ConcurrentHashMap in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. ConcurrentHashMap in Java With Examples
  2. Difference Between ArrayList And CopyOnWriteArrayList in Java
  3. Java Semaphore With Examples
  4. Java ReentrantLock With Examples
  5. Java Concurrency Interview Questions And Answers

You may also like-

  1. Difference Between Comparable and Comparator in Java
  2. How to Sort HashSet in Java
  3. Lambda Expression Examples in Java
  4. How to Iterate a HashMap of ArrayLists of String in Java
  5. Race Condition in Java Multi-Threading
  6. Interface Default Methods in Java
  7. Java ThreadLocal Class With Examples
  8. How to Read File From The Last Line in Java