Last Updated : 14 Feb, 2025
In Java, the ConcurrentHashMap is a thread-safe implementation of the Map interface. It allows multiple threads to read and write data simultaneously, without the need for locking the entire map. Unlike a regular HashMap, which is not thread-safe, ConcurrentHashMap ensures that the operations are thread-safe, making it ideal for scenarios where multiple threads need to access and modify the map concurrently.
Example: This example demonstrates basic operations like adding, retrieving, and removing elements on a ConcurrentHashMap in a multi-threaded environment.
Java
// Java Program to demosntrates the
// working of ConcurrentHashMap
import java.util.concurrent.ConcurrentHashMap;
public class Geeks {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
// Adding elements to the map
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
System.out.println("Map size: " + map.size());
// Getting values from the map
System.out.println("Value of A: " + map.get("A"));
// Removing elements from the map
map.remove("B");
System.out.println("After removal map size is: " + map.size());
}
}
Map size: 3 Value of A: 1 After removal map size is: 2ConcurrentHashMap Hierarchy
It implements Serializable, ConcurrentMap<K, V>, Map<K, V> interfaces and extends AbstractMap<K, V> class.
Declaration of ConcurrentHashMapIn Java, the declaration of ConcurrentHashMap can be done as:
ConcurrentHashMap<KeyType, ValueType> map = new ConcurrentHashMap<>();
Note: Here the KeyType is the type of the key in the map and ValueType is the type of values associated with the keys.
ConstructorsConstructor
Description
ConcurrentHashMap()
This constructor creates a new, empty map with a default initial capacity (16), load factor (0.75) and concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity)
This constructor creates a new, empty map with the specified initial capacity, and with default load factor (0.75) and concurrencyLevel (16).
ConcurrentHashMap(int initialCapacity, float loadFactor)
This constructor creates a new, empty map with the specified initial capacity and load factor and with the default concurrencyLevel (16)
ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
This constructor creates a new, empty map with the specified initial capacity, load factor, and concurrency level.
ConcurrentHashMap(Map m)
This constructor creates a new map with the same mappings as the given map.
Example: This example demonstrates the usage of various ConcurrentHashMap methods.
Java
// Java program to demonstrate working of various methods
// like put(), putIfAbsent(), remove() and replace()
import java.util.concurrent.*;
class Geeks {
public static void main(String[] args)
{
// create an instance of
// ConcurrentHashMap
ConcurrentHashMap<Integer, String> m
= new ConcurrentHashMap<>();
// Insert mappings using
// put method
m.put(100, "Hello");
m.put(101, "Geeks");
m.put(102, "Geeks");
// Here we cant add Hello because 101 key
// is already present in ConcurrentHashMap object
m.putIfAbsent(101, "Hello");
// We can remove entry because 101 key
// is associated with For value
m.remove(101, "Geeks");
// Now we can add Hello
m.putIfAbsent(103, "Hello");
// We cant replace Hello with For
m.replace(101, "Hello", "For");
System.out.println(m);
}
}
{100=Hello, 102=Geeks, 103=Hello}Performing Various Operations on ConcurrentHashMap
1. Adding Elements: We can use put() or putAll() to insert element to a ConcurrentHashMap.
Example: This example demonstrates creating a ConcurrentHashMap, adding elements to it, and then copying its contents to another ConcurrentHashMap.
Java
// Java Program to demonstrates
// the working of put()
import java.util.concurrent.*;
public class Geeks{
public static void main(String[] args) {
// Create ConcurrentHashMap
ConcurrentHashMap<String, String> map1 = new ConcurrentHashMap<>();
// Add elements to map
map1.put("1", "One");
map1.put("2", "Two");
map1.put("3", "Three");
map1.put("4", "Four");
map1.put("5", "Five");
map1.put("6", "Six");
// Print map1
System.out.println("map1: " + map1);
// Create a new map and copy all entries from map1
ConcurrentHashMap<String, String> map2 = new ConcurrentHashMap<>(map1);
// Print map2
System.out.println("map2: " + map2);
}
}
map1: {1=One, 2=Two, 3=Three, 4=Four, 5=Five, 6=Six} map2: {1=One, 2=Two, 3=Three, 4=Four, 5=Five, 6=Six}
2. Removing Elements: We can use the remove() method to remove elements from the ConcurrentHashMap. To clear the entire map, we can use the clear().
Example: This example demonstrates adding elements to a ConcurrentHashMap removing entries by key, and clearing the map.
Java
// Java program to demonstrate removing
// elements from ConcurrentHashMap
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class Geeks{
public static void main(String[] args)
{
// Creating ConcurrentHashMap
Map<String, String> m
= new ConcurrentHashMap<String, String>();
// Adding elements to the map
// using put() method
m.put("1", "One");
m.put("2", "Two");
m.put("3", "Three");
m.put("4", "Four");
m.put("5", "Five");
m.put("6", "Six");
System.out.println("Map: " + m);
System.out.println();
// Removing the mapping
// with existing key 6
// using remove() method
String s = m.remove("6");
// Printing the map after remove()
System.out.println(
"After removing mapping with key 6:");
System.out.println("Map: " + m);
System.out.println("Value removed: "
+ s);
System.out.println();
// Removing the mapping
// with non-existing key 10
// using remove() method
s = m.remove("10");
// Printing the map after remove()
System.out.println(
"After removing mapping with key 10:");
System.out.println("Map: " + m);
System.out.println("Value removed: "
+ s);
System.out.println();
// Now clear the map using clear()
m.clear();
System.out.println("Map after use of clear(): "
+ m);
}
}
Output:
3. Accessing Elements: We can use the get() method to access the elements of the ConcurrentHashMap.
Example: This example demonstrates inserting elements into a ConcurrentHashMap and retrieving values using the get() method based on specific keys.
Java
// Java Program Demonstrate accessing
// elements of ConcurrentHashMap
import java.util.concurrent.*;
class Geeks {
public static void main(String[] args)
{
// create an instance of ConcurrentHashMap
ConcurrentHashMap<Integer, String> m
= new ConcurrentHashMap<Integer, String>();
// insert mappings using put method
m.put(100, "Geeks");
m.put(101, "for");
m.put(102, "Geeks");
m.put(103, "Contribute");
// Displaying the HashMap
System.out.println("ConcurrentHashMap:" + m);
// Display the value of 100
System.out.println("The Value associated to "
+ "100 is : " + m.get(100));
// Getting the value of 103
System.out.println("The Value associated to "
+ "103 is : " + m.get(103));
}
}
ConcurrentHashMap:{100=Geeks, 101=for, 102=Geeks, 103=Contribute} The Value associated to 100 is : Geeks The Value associated to 103 is : Contribute
4. Iterating Elements: We can use the Iterator interface to traverse over the elements of ConcurrentHashMap.
Example: This example demonstrates iterating over a ConcurrentHashMap using an iterator to access both keys and values.
Java
// Java Program for traversing a
// ConcurrentHashMap
import java.util.*;
import java.util.concurrent.*;
public class Geeks {
public static void main(String[] args)
{
// create an instance of ConcurrentHashMap
ConcurrentHashMap<Integer, String> m
= new ConcurrentHashMap<Integer, String>();
// Add elements using put()
m.put(8, "Third");
m.put(6, "Second");
m.put(3, "First");
m.put(11, "Fourth");
// Create an Iterator over the
// ConcurrentHashMap
Iterator<ConcurrentHashMap.Entry<Integer, String> >
i = m.entrySet().iterator();
// The hasNext() method is used to check if there is
// a next element The next() method is used to
// retrieve the next element
while (i.hasNext()) {
ConcurrentHashMap.Entry<Integer, String> entry
= i.next();
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
}
Key = 3, Value = First Key = 6, Value = Second Key = 8, Value = Third Key = 11, Value = FourthMethods
Methods
Description
clear() Removes all of the mappings from this map. compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null. computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value. contains(Object value) Tests if some keymaps into the specified value in this table. containsKey(Object key) Tests if the specified object is a key in this table. containsValue(Object value) Returns true if this map maps one or more keys to the specified value. elements() Returns an enumeration of the values in this table. entrySet() Returns a Set view of the mappings contained in this map. equals(Object o) Compares the specified object with this map for equality. forEach(long parallelismThreshold, BiConsumer<? super K,? super V> action) Performs the given action for each (key, value). forEach(long parallelismThreshold, BiFunction<? super K,? super V,? extends U> transformer, Consumer<? super U> action) Performs the given action for each non-null transformation of each (key, value). forEachEntry(long parallelismThreshold, Consumer<? super Map.Entry<K,V>> action) Performs the given action for each entry. forEachEntry(long parallelismThreshold, Function<Map.Entry<K,V>,? extends U> transformer, Consumer<? super U> action) Performs the given action for each non-null transformation of each entry. forEachKey(long parallelismThreshold, Consumer<? super K> action) Performs the given action for each key. forEachKey(long parallelismThreshold, Function<? super K,? extends U> transformer, Consumer<? super U> action) Performs the given action for each non-null transformation of each key. forEachValue(long parallelismThreshold, Consumer<? super V> action) Performs the given action for each value. forEachValue(long parallelismThreshold, Function<? super V,? extends U> transformer, Consumer<? super U> action) Performs the given action for each non-null transformation of each value. get(Object key) Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. getOrDefault(Object key, V defaultValue) Returns the value to which the specified key is mapped, or the given default value if this map contains no mapping for the key. hashCode() Returns the hash code value for this Map, i.e., the sum of, for each key-value pair in the map, key.hashCode() ^ value.hashCode(). keys() Returns an enumeration of the keys in this table. keySet() Returns a Set view of the keys contained in this map. keySet(V mappedValue) Returns a Set view of the keys in this map, using the given common mapped value for any additions (i.e., Collection.add(E) and Collection.addAll(Collection)). mappingCount() Returns the number of mappings. merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) If the specified key is not already associated with a (non-null) value, associates it with the given value. newKeySet() Creates a new Set backed by a ConcurrentHashMap from the given type to Boolean.TRUE. newKeySet(int initialCapacity) Creates a new Set backed by a ConcurrentHashMap from the given type to Boolean.TRUE. put(K key, V value) Maps the specified key to the specified value in this table. putAll(Map<? extends K,? extends V> m) Copies all of the mappings from the specified map to this one. putIfAbsent(K key, V value) If the specified key is not already associated with a value, associates it with the given value. reduce(long parallelismThreshold, BiFunction<? super K,? super V,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer) Returns the result of accumulating the given transformation of all (key, value) pairs using the given reducer to combine values, or null if none. reduceEntries(long parallelismThreshold, BiFunction<Map.Entry<K,V>,Map.Entry<K,V>,? extends Map.Entry<K,V>> reducer) Returns the result of accumulating all entries using the given reducer to combine values, or null if none. reduceEntries(long parallelismThreshold, Function<Map.Entry<K,V>,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer) Returns the result of accumulating the given transformation of all entries using the given reducer to combine values, or null if none. reduceEntriesToDouble(long parallelismThreshold, ToDoubleFunction<Map.Entry<K,V>> transformer, double basis, DoubleBinaryOperator reducer) Returns the result of accumulating the given transformation of all entries using the given reducer to combine values, and the given basis as an identity value. reduceEntriesToInt(long parallelismThreshold, ToIntFunction<Map.Entry<K,V>> transformer, int basis, IntBinaryOperator reducer) Returns the result of accumulating the given transformation of all entries using the given reducer to combine values, and the given basis as an identity value. reduceEntriesToLong(long parallelismThreshold, ToLongFunction<Map.Entry<K,V>> transformer, long basis, LongBinaryOperator reducer) Returns the result of accumulating the given transformation of all entries using the given reducer to combine values, and the given basis as an identity value. reduceKeys(long parallelismThreshold, BiFunction<? super K,? super K,? extends K> reducer) Returns the result of accumulating all keys using the given reducer to combine values, or null if none. reduceKeys(long parallelismThreshold, Function<? super K,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer) Returns the result of accumulating the given transformation of all keys using the given reducer to combine values, or null if none. reduceKeysToDouble(long parallelismThreshold, ToDoubleFunction<? super K> transformer, double basis, DoubleBinaryOperator reducer) Returns the result of accumulating the given transformation of all keys using the given reducer to combine values, and the given basis as an identity value. reduceKeysToInt(long parallelismThreshold, ToIntFunction<? super K> transformer, int basis, IntBinaryOperator reducer) Returns the result of accumulating the given transformation of all keys using the given reducer to combine values, and the given basis as an identity value. reduceKeysToLong(long parallelismThreshold, ToLongFunction<? super K> transformer, long basis, LongBinaryOperator reducer) Returns the result of accumulating the given transformation of all keys using the given reducer to combine values, and the given basis as an identity value. reduceToDouble(long parallelismThreshold, ToDoubleBiFunction<? super K,? super V> transformer, double basis, DoubleBinaryOperator reducer) Returns the result of accumulating the given transformation of all (key, value) pairs using the given reducer to combine values, and the given basis as an identity value. reduceToInt(long parallelismThreshold, ToIntBiFunction<? super K,? super V> transformer, int basis, IntBinaryOperator reducer) Returns the result of accumulating the given transformation of all (key, value) pairs using the given reducer to combine values, and the given basis as an identity value. reduceToLong(long parallelismThreshold, ToLongBiFunction<? super K,? super V> transformer, long basis, LongBinaryOperator reducer) Returns the result of accumulating the given transformation of all (key, value) pairs using the given reducer to combine values, and the given basis as an identity value. reduceValues(long parallelismThreshold, BiFunction<? super V,? super V,? extends V> reducer) Returns the result of accumulating all values using the given reducer to combine values, or null if none. reduceValues(long parallelismThreshold, Function<? super V,? extends U> transformer, BiFunction<? super U,? super U,? extends U> reducer) Returns the result of accumulating the given transformation of all values using the given reducer to combine values, or null if none. reduceValuesToDouble(long parallelismThreshold, ToDoubleFunction<? super V> transformer, double basis, DoubleBinaryOperator reducer) Returns the result of accumulating the given transformation of all values using the given reducer to combine values, and the given basis as an identity value. reduceValuesToInt(long parallelismThreshold, ToIntFunction<? super V> transformer, int basis, IntBinaryOperator reducer) Returns the result of accumulating the given transformation of all values using the given reducer to combine values, and the given basis as an identity value. reduceValuesToLong(long parallelismThreshold, ToLongFunction<? super V> transformer, long basis, LongBinaryOperator reducer) Returns the result of accumulating the given transformation of all values using the given reducer to combine values, and the given basis as an identity value. remove(Object key) Removes the key (and its corresponding value) from this map. remove(Object key, Object value) Removes the entry for a key only if currently mapped to a given value. replace(K key, V value) Replaces the entry for a key only if currently mapped to some value. replace(K key, V oldValue, V newValue) Replaces the entry for a key only if currently mapped to a given value. search(long parallelismThreshold, BiFunction<? super K,? super V,? extends U> searchFunction) Returns a non-null result from applying the given search function on each (key, value), or null if none. searchEntries(long parallelismThreshold, Function<Map.Entry<K,V>,? extends U> searchFunction) Returns a non-null result from applying the given search function on each entry, or null if none. searchKeys(long parallelismThreshold, Function<? super K,? extends U> searchFunction) Returns a non-null result from applying the given search function on each key, or null if none. searchValues(long parallelismThreshold, Function<? super V,? extends U> searchFunction) Returns a non-null result from applying the given search function on each value, or null if none. toString() Returns a string representation of this map. values() Returns a Collection view of the values contained in this map. Methods Declared in Class java.util.AbstractMapMethod
Description
clone() Returns a shallow copy of this AbstractMap instance: the keys and values themselves are not cloned. isEmpty() Returns true if this map contains no key-value mappings. size() Returns the number of key-value mappings in this map. Methods Declared in Interface java.util.concurrent.ConcurrentMapMethod
Description
forEach(BiConsumer<? super K,? super V> action) Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. replaceAll(BiFunction<? super K,? super V,? extends V> function) Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. ConcurrentHashMap vs HashtableHashTable:
ConcurrentHashMap:
Map ht = new Hashtable();
Map chm = new ConcurrentHashMap();
Is Null Key Allowed ?No
No
Is Null Value Allowed ?No
No (does not allow either null keys or values)
Is Thread Safe ?Yes
Yes, Thread safety is ensured by having separate locks for separate buckets, resulting in better performance. Performance is further improved by providing read access concurrently without any blocking. PerformanceSlow due to synchronization overhead.
Faster than Hashtable. ConcurrentHashMap is a better choice when there are more reads than writes. Iterator Hashtable uses enumerator to iterate the values of Hashtable object. Enumerations returned by the Hashtable keys and elements methods are not fail-fast. Fail-safe iterator: Iterator provided by the ConcurrentHashMap is fail-safe, which means it will not throw ConcurrentModificationException.Note: If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
AdvantagesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4