VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-implement-concurrenthashmap-api/

⇱ Java Program to Implement ConcurrentHashMap API - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Implement ConcurrentHashMap API

Last Updated : 23 Jul, 2025

ConcurrentHashMap class obeys the same functional specification as HashTable and includes all the versions of methods corresponding to each method of a HashTable. A HashTable supports the full concurrency of retrievals and adjustable concurrency for updates. All the operations of ConcurrentHashMap are thread-safe but the retrieval operations do not entail locking i.e. it does not support locking the entire table in a way that prevents all access. This API is fully interoperable to the HashTable in the programs. It is present in java.util.concurrent package.

ConcurrentHashMap extends AbstractMap<K,V> and Object classes. This API does not allow null to be used as a key or value and it is a member of Java Collections Framework.

Type Parameters:

  • K - the type of keys maintained by the map
  • V - the type of values mapped to it.

All implemented interfaces:

Serializable, ConcurrentMap<K,V>, Map<K,V>

Syntax:

public class ConcurrentHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable

Constructors:

  • ConcurrentHashMap() - Creates an empty Map with an initial capacity of 16, load factor of 0.75 and concurrency level of 16.
  • ConcurrentHashMap(int initialCapacity) - Creates an empty Map with the given initial capacity and default values of load factor and concurrencyLevel.
  • ConcurrentHashMap(int initialCapacity, float loadFactor) - Creates an empty Map with the given initial capacity and given load factor and default concurrencyLevel.
  • ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) - Creates an empty Map with given initial capacity, load factor and concurrencyLevel.
  • ConcurrentHashMap(Map<? extends K,? extends V> m) - Creates a new Map with the same mappings as given in the Map.

Code:


Output
The Keys of the ConcurrentHashMap is 
1 2 3 4 
The values of the ConcurrentHashMap is 
Amit Ankush Akshat Tarun 
The entry set of the ConcurrentHashMap is 
1=Amit 
2=Ankush 
3=Akshat 
4=Tarun 
The ConcurrentHashMap contains Key 3 :true
The ConcurrentHashMap contains Tarun:true
Put the key 10 with value Shikha if not associated : null
Replace key 3 oldvalue of Akshat and newvalue Pari :true
The Size of the ConcurrentHashMap is 5
The ConcurrentHashMap is empty
Comment