How ConcurrentHashMap works in Java?
and difference between a synchronized map and concurrentHashMap
Hello guys, how HashMap work in Java, and How ConcurrentHashMap works in Java are two of the popular questions in Java interviews. I have seen it many times and even asked about it a couple of times.
Since a lot of you requested me to share core Java interview questions, I have decided to write at least one post about them every week and in this post, I will talk about How ConcurrentHashMap work and what is difference between a synchronized Map and ConcurrentHashMap in Java.
ConcurrentHashMap is a class in Java that provides a thread-safe implementation of the Map interface. It was introduced in Java 5 to address the need for a concurrent and scalable map data structure. The primary goal ConcurrentHashMap is to allow multiple threads to read and write to the map concurrently without the need for external synchronization.
Here are some key features and aspects of how ConcurrentHashMap works:
Segmentation:
ConcurrentHashMapis divided into segments, and each segment acts as an independent hash table. The number of segments is determined by theconcurrencyLevelspecified during the creation of theConcurrentHashMap. Each segment is essentially a separate hash table, and different segments can be operated on concurrently by different threads.Here is how a segment looks like:
Keep reading with a 7-day free trial
Subscribe to Javarevisited Newsletter to keep reading this post and get 7 days of free access to the full post archives.

