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}