java.util.WeakHashMap.putAll()方法实例
putAll(Map<? extends K,? extends V> m) 方法用于所有从指定映射中的映射关系复制到此映射。这些映射关系将替换此映射目前针对指定映射的所有键的所有映射关系。
声明
以下是java.util.WeakHashMap.putAll()方法的声明。
public void putAll(Map<? extends K,? extends V> m)
参数
-
m--这是将要存储在此映射的映射。
返回值
NA
异常
-
NullPointerException--如果指定映射为null,则抛出该异常。
例子
下面的例子显示java.util.WeakHashMap.putAll()方法的使用。
package com.yiibai; 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); } }
现在编译和运行上面的代码示例,将产生以下结果。
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}