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 − 如果指定的映射为空,则抛出此异常。


示例

下面的例子展示了 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}