Concurrent Hash Map in Java with Details

In Simple term it allows concurrent access to Map by multiple threads.

  • Why other ConcurrentHashMap ?
  • How it is different?
  • How ConcurrentHashMap works internally?
  • Concurrency Level

From Java 5 onwards ConcurrentHashMap is introduced as an alternate for HashTable. In traditional way by using Collections.synchronizedMap() we were getting synchronizedMap but there’re a disadvantage of this method i.e. poor performance as this only allow single thread access. All these issue has been addresses as part of ConcurrentHashMap.

Why other ConcurrentHashMap?

while we have already got HashMap, HashTable then what’s a desire of ConcurrentHashMap, it’s because it provides better performance at the identical time it’s thread-safe.

How ConcurrentHashMap is different?

It’s also supported hashing but its performance is improved by its locking strategy. Unlike HashTable or Synchronized HashMap it doesn’t apply the identical lock on each method it uses a separate lock for every method, It uses re-entrant lock for this purpose. As like other classes in Map example HashMap, ConcurrentHashMap has 16 buckets. Here we have segments, to make ConcurrentHashMap with over 16 buckets.

How ConcurrentHashMap works internally:

  • This map allows concurrent thread access.
  • Only a part of the map called segment i.e. underlying arrangement is getting locked while adding or updating the map.
  • It allows concurrent thread access to read the info without locking.
  • it had been introduced to boost performance.


  • Load-Factor: It is a value which is used to control the resizing factor.
  • Initial Capacity: It is a property which creates a Map with the size provided.
  • Let’s see below diagram and try to understand how ConcurrentHashMap works.


So within the above diagram, we’ve 16 locks which lock only a little of the map which is required so other methods are often assessed by different threads thus improving performance.

Just like HashMap, ConcurrentHashMap works in an exceedingly similar way it contains 16 segments by default and stores element by hashing so if the weather have the identical hash they’re stored on the identical segment as shown within the diagram above with the assistance of the linked list.

ConcurrentHashMap Consctructor:

  • ConcurrentHashMap m=new ConcurrentHashMap();
  • ConcurrentHashMap m=new ConcurrentHashMap(int initialCapacity);
  • ConcurrentHashMap m=new ConcurrentHashMap(int initialCapacity, float loadFactor);
  • ConcurrentHashMap m=new ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel);
  • ConcurrentHashMap m=new ConcurrentHashMap(Map m);

Let’s see more:

%d bloggers like this: