java.util.WeakHashMap.putAll()


描述

所述putAll(Map<? extends K,? extends V> m) 方法用于所有映射的从指定映射到这个map.These映射复制将替换此地图中有任何键的目前任何映射指定地图。

声明

以下是java.util.WeakHashMap.putAll()方法的声明。

public void putAll(Map<? extends K,? extends V> m)

参数

m - 这是要存储在此映射中的映射。

返回值

NA

异常

NullPointerException - 如果指定的映射为null,则抛出此异常。

实例

以下示例显示了java.util.WeakHashMap.putAll()方法的用法。

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);
   }     
}

让我们编译并运行上面的程序,这将产生以下结果。

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}