Java ConcurrentHashMap

asked Apr 22, 2015 in Core java by rajesh
0 votes

1 Answer

0 votes

Java ConcurrentHashMap

This Java tutorial is part of our Java concurrent collections tutorial series. In this tutorial we will see about Java ConcurrentHashMap.

ConcurrentHashMap is a hash table which supports complete concurrency for retrievals and updates. ConcurrentHashMap follows the specifications of a Hashtable. ConcurrentHashMap does not lock the entire collection for synchronization. ConcurrentHashMap is a suited candidate collection when there are high number of updates and less number of read concurrently.

ConcurrentHashMap implements ConcurrentMap which lays the blue print for the concurrent operations. This was introduced part of the JDK 1.5 in the Java collections framework.

ConcurrentHashMap Initialization

ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
ConcurrentHashMap comes with multiple constructors and above being one among them. initialCapacity is to fix the internal size of the collection. Hash table density is decided based on the loadFactor. Now this is new, concurrencyLevel. By default ConcurrentHashMap allows 16 number of concurrent threads. We can change this number using the concurrencyLevl argument. Most of the times 16 should be sufficient and playing with these number may cause undesirable performance issues. Know before you set these arguments.


ConcurrentHashMap does not throw ConcurrentModificationException if the underlying collection is modified during an iteration is in progress. Iterators may not reflect the exact state of the collection if it is being modified concurrently. It may reflect the state when it was created and at some moment later. The fail-safe property is given a guarantee based on this.

Which One is Best?

ConcurrentHashMap can be considered as an alternative to Hashtable. It is a better in comparison with a Hashtable and a synchronized Map. ConcurrentHashMap blocks only the parts as required and provides ultimate concurrency. ConcurrentHashMap by default is separated into 16 regions and locks are applied. This default number can be set while initializing a ConcurrentHashMap instance.

Key Points to Remember on ConcurrentHashMap

ConcurrentHashMap only locks a portion of the collection on update.
ConcurrentHashMap is better than Hashtable and synchronized Map.
ConcurrentHashMap is failsafe does not throws ConcurrentModificationException.
null is not allowed as a key or value in ConcurrentHashMap.
Level of concurrency can be chosen by the programmer on a ConcurrentHashMap while initializing it.
ConcurrentHashMap Example


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

public class ConcurrentHashMapExample {
    public static void main(String[] args) {

        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
        for (Map.Entry e : concurrentHashMap.entrySet()) {
            System.out.println(e.getKey() + " = " + e.getValue());

answered Apr 22, 2015 by rajesh