![]() |
VOOZH | about |
The putAll() method in Java's ConcurrentHashMap class is used to copy all the key-value mappings from a specified map to the current map. It has the following signature:
void putAll(Map<? extends K,? extends V> m)
where:
m is the map whose key-value mappings are to be copied to the current map.
The putAll() method works in a concurrent environment, which means that it can be called from multiple threads without causing any data race or synchronization issues.
When a putAll() operation is performed, the method first acquires the lock on all the segments of the current map and then copies all the key-value mappings from the specified map to the current map.
Combined map: {Bob=30, Alice=25, Charlie=35, Dave=40}The java.util.concurrent.ConcurrentHashMap.putAll() is an in-built function in Java that is used for the copy operation. The method copies all of the elements i.e., the mappings, from one ConcurrentHashMap into another.
Syntax:
new_conn_hash_map.putAll(conn_hash_map)
Parameters: The function accepts a ConcurrentHashMap conn_hash_map as its only parameter and copies all its mappings with this map.
Return Value: The method does not return any values.
Exception: The function throws NullPointerException when the specified parameter is null. Below programs illustrate the ConcurrentHashMap.putAll() method :
Program 1: This program involves mapping String Values to Integer Keys.
Initial Mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
New mappings are: {100=Geeks, 101=for, 102=Geeks, 103=Gfg, 104=GFG}
Program 2: This program involves mapping Integer Values to String Keys.
Initial Mappings are: {Gfg=100, GFG=102, GfG=18, gfg=15, gfG=55}
New mappings are: {Gfg=100, GFG=102, GfG=18, gfg=15, gfG=55, gFg=22}