The putAll(Map<? extends K,? extends V> m) method is used to copy all of the mappings from the specified map to this map.These mappings will replace any mappings that this map had for any of the keys currently in the specified map.
Following is the declaration for java.util.WeakHashMap.putAll() method.
public void putAll(Map<? extends K,? extends V> m)
m − This is the mappings to be stored in this map.
NA
NullPointerException − This exception is thrown if the specified map is null.
The following example shows the usage of java.util.WeakHashMap.putAll() method.
package com.tutorialspoint; import java.util.Map; import java.util.WeakHashMap; public class WeakHashMapDemo { public static void main(String[] args) { Map<String, String> weakHashMapOne = new WeakHashMap<String, String>(); Map<String, String> weakHashMapTwo = new WeakHashMap<String, String>(); // put keys and values in the Map System.out.println("Populating two Maps"); weakHashMapOne.put("1", "first"); weakHashMapOne.put("2", "two"); weakHashMapOne.put("3", "three"); weakHashMapTwo.put("1", "1st"); weakHashMapTwo.put("2", "2nd"); weakHashMapTwo.put("3", "3rd"); // checking Map System.out.println("Before - Map 1: "+weakHashMapOne); System.out.println("Before - Map 2: "+weakHashMapTwo); // putting map 2 into map1 weakHashMapOne.putAll(weakHashMapTwo); System.out.println("After - Map 1: "+weakHashMapOne); System.out.println("After - Map 2: "+weakHashMapTwo); } }
Let us compile and run the above program, this will produce the following result.
Putting values into the Map Before - Map 1: {1=first, 2=two, 3=three} Before - Map 2: {1=1st, 2=2nd, 3=3rd} After - Map 1: {1=1st, 2=2nd, 3=3rd} After - Map 2: {1=1st, 2=2nd, 3=3rd}