ConcurrentHashMap putAll() method in Java
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.
Here is an example of using the putAll() method:
Java
import java.util.concurrent.ConcurrentHashMap; import java.util.HashMap; import java.util.Map; public class ConcurrentHashMapExample { public static void main(String[] args) { ConcurrentHashMap<String, Integer> map1 = new ConcurrentHashMap<>(); map1.put( "Alice" , 25 ); map1.put( "Bob" , 30 ); Map<String, Integer> map2 = new HashMap<>(); map2.put( "Charlie" , 35 ); map2.put( "Dave" , 40 ); map1.putAll(map2); System.out.println( "Combined map: " + map1); } } |
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.
Java
// Java Program Demonstrate putAll() // method of ConcurrentHashMap import java.util.concurrent.*; class ConcurrentHashMapDemo { public static void main(String[] args) { ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>(); chm.put( 100 , "Geeks" ); chm.put( 101 , "for" ); chm.put( 102 , "Geeks" ); chm.put( 103 , "Gfg" ); chm.put( 104 , "GFG" ); // Displaying the existing HashMap System.out.println( "Initial Mappings are: " + chm); ConcurrentHashMap<Integer, String> new_chm = new ConcurrentHashMap<Integer, String>(); new_chm.putAll(chm); // Displaying the new map System.out.println( "New mappings are: " + new_chm); } } |
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.
Java
// Java Program Demonstrate putAll() // method of ConcurrentHashMap import java.util.concurrent.*; class ConcurrentHashMapDemo { public static void main(String[] args) { ConcurrentHashMap<String, Integer> chm = new ConcurrentHashMap<String, Integer>(); chm.put( "Gfg" , 100 ); chm.put( "GFG" , 102 ); chm.put( "GfG" , 18 ); chm.put( "gfg" , 15 ); chm.put( "gfG" , 55 ); // Displaying the existing HashMap System.out.println( "Initial Mappings are: " + chm); ConcurrentHashMap<String, Integer> new_chm = new ConcurrentHashMap<String, Integer>(); // Copying the contents new_chm.putAll(chm); // Inserting a new mapping new_chm.put( "gFg" , 22 ); // Displaying the new map System.out.println( "New mappings are: " + new_chm); } } |
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}
Please Login to comment...