Difference Between ConcurrentHashMap and SynchronizedHashMap - GeeksforGeeks

14 Aug, 2021
The ConcurrentHashMap and SynchronizedHashMap both are the Collection classes which are thread-safe and can be used in multithreaded and concurrent java application. But there are few differences that exists between them. In this article, we have tried to cover all these differences between them.
1. ConcurrentHashMap: ConcurrentHashMap is a class which implements the ConcurrentMap interface. It uses Hashtable, underlined data structure. As we know, while dealing with thread in our application HashMap is not a good choice because of the performance issue. To resolve this issue, we use ConcurrentHashMap in our application.  ConcurrentHashMap is thread-safe therefore multiple threads can operate on a single object without any problem. In ConcurrentHashMap, the Object is divided into a number of segments according to the concurrency level. By default, it allows 16 thread to read and write from the Map without any synchronization. In ConcurrentHashMap, at a time any number of threads can perform retrieval operation but for updating in the object, the thread must lock the particular segment in which the thread wants to operate. This type of locking mechanism is known as Segment locking or bucket locking. Hence, at a time16 update operations can be performed by threads.
ConcurrentHashMap Demo:
Java
// Java Program to demonstrate the// working of ConcurrentHashMap import java.util.*;import java.util.concurrent.*; public class TraversingConcurrentHashMap {     public static void main(String[] args)    {         // create an instance of ConcurrentHashMap        ConcurrentHashMap<Integer, String> chmap            = new ConcurrentHashMap<Integer, String>();         // Add elements using put()        chmap.put(10, "Geeks");        chmap.put(20, "for");        chmap.put(30, "Geeks");        chmap.put(40, "Welcome");        chmap.put(50, "you");         // Create an Iterator over the        // ConcurrentHashMap        Iterator<ConcurrentHashMap.Entry<Integer, String> >            itr = chmap.entrySet().iterator();         // The hasNext() method is used to check if there is        // a next element and the next() method is used to        // retrieve the next element        while (itr.hasNext()) {            ConcurrentHashMap.Entry<Integer, String> entry                = itr.next();            System.out.println("Key = " + entry.getKey()                               + ", Value = "                               + entry.getValue());        }    }}
  
Key = 50, Value = you
Key = 20, Value = for
Key = 40, Value = Welcome
Key = 10, Value = Geeks
Key = 30, Value = Geeks
 
2. Synchronized HashMap: Java HashMap is a non-synchronized collection class. If we need to perform thread-safe operations on it then we must need to synchronize it explicitly. The synchronizedMap() method of java.util.Collections class is used to synchronize it. It returns a synchronized (thread-safe) map backed by the specified map. 
Synchronized HashMap Demo: 
Java
// Java program to demonstrate the// working of Synchronized HashMap import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set; public class SynchronizedHashMap {    public static void main(String args[])    {         // Creating a HashMap        HashMap<Integer, String> hmap            = new HashMap<Integer, String>();         // Adding the elements using put method        hmap.put(10, "Geeks");        hmap.put(20, "for");        hmap.put(30, "Geeks");        hmap.put(25, "Welcome");        hmap.put(40, "you");         // Creating a synchronized map        Map map = Collections.synchronizedMap(hmap);        Set set = map.entrySet();         // Synchronize on HashMap, not on set        synchronized (map)        {            Iterator i = set.iterator();            // Printing the elements            while (i.hasNext()) {                Map.Entry me = (Map.Entry)i.next();                System.out.print(me.getKey() + ": ");                System.out.println(me.getValue());            }        }    }}
20: for
40: you
25: Welcome
10: Geeks
30: Geeks
Difference between ConcurrentHashMap and Synchronized HashMap:
                  ConcurrentHashMap
                      Synchronized HashMap
simmytarika5
Java-Collections
Java-ConcurrentHashMap
Technical Scripter 2020
Difference Between
Java
Technical Scripter
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference between Process and Thread
Difference Between Method Overloading and Method Overriding in Java
Difference between Clustered and Non-clustered index
Differences between IPv4 and IPv6
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Reverse a string in Java
Arrays.sort() in Java with examples