Difference Between Concurrent HashMap and Synchronized HashMap in Java



A Map is an object that stores key-value pairs, where each key is unique but values can repeat. The HashMap is a type of Map that uses a hashtable in order to store these pairs. Now we will discuss the differences between ConcurrentHashMap and Synchronized Hashmap.

What is a ConcurrentHashMap?

ConcurrentHashMap is a class that was introduced in jdk1.5.  ConcurrentHashMap applies locks only at bucket level called fragment while adding or updating the map. So, aConcurrentHashMap allows concurrent read and write operation to the map. 

Example of ConcurrentHashMap

The following is an example of ConcurrentHashMap in java ?

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapExample {
   public static void main(String[] args) {
      // ConcurrentHashMap
      Map<Integer, String> laptopmap = new ConcurrentHashMap<Integer, String>();
      laptopmap.put(1, "IBM");
      laptopmap.put(2, "Dell");
      laptopmap.put(3, "HCL");
      System.out.println("ConcurrentHashMap is: " + laptopmap);
   }
}

The output of the above Java program is ?

ConcurrentHashMap is: {1=IBM, 2=Dell, 3=HCL}

What is a Synchronized hashmap?

Synchronized hashmap(Collection.syncronizedHashMap()) is a method of Collection framework. This method applies a lock on the entire collection. So, if one thread is accessing the map then no other thread can access the same map. 

Example of Synchronized Hashmap

The following is an example of Synchronized Hashmap in java ?

import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

public class SynchronizedMapExample {
   public static void main(String[] args) {
      Map laptopmap = new HashMap();
      laptopmap.put(1, "IBM");
      laptopmap.put(2, "Dell");
      laptopmap.put(3, "HCL");

      // create a synchronized map
      Map syncmap = Collections.synchronizedMap(laptopmap);
      System.out.println("Synchronized map is : " + syncmap);
   }
}

The output of the above Java program is ?

Synchronized map is : {1=IBM, 2=Dell, 3=HCL}

Difference between ConcurrentHashMap and Synchronized hashmap

The following table shows the difference between Concurrent Hashmap and Synchronized hashmap ?

Sr. No. Key ConcurrentHashMap Synchronized hashmap
1
Implementation
It is a class that implements a ConcurrentHashMap and serializable interface. 
It is a method in Collection class.  
2
Lock mechanism
Locks the portion
Locks the whole map. 
3
Performance
ConcurrentHashMap allows concurrent read and write. So performance is relatively better than a synchronized map. 
Multiple threads can't access the map concurrently. So, performance is relatively less than a ConcurrentHashMap.
4
Null key
It doesn't allow null as a key or value. 
It allows null as a key.

Concurrent modification exception
It doesn't throw concurrent modification exception. 
Iterator return by synchronized map throws concurrent modification exception
Updated on: 2025-04-15T19:10:46+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements