void putAll(Map<? extends K,? extends V> map)
描述 (Description)
putAll(Map《? extends K,? extends V》 map)方法用于将指定地图中的所有映射复制到此地图。 这些映射将替换此映射对当前位于指定映射中的任何键的任何映射。
声明 (Declaration)
以下是java.util.TreeMap.putAll()方法的声明。
public void putAll(Map<? extends K,? extends V> map)
参数 (Parameters)
map - 这是要存储在此映射中的映射。
返回值 (Return Value)
NA
异常 (Exception)
ClassCastException - 如果指定映射中的键或值的类阻止它存储在此映射中,则抛出此异常。
NullPointerException - 如果指定的映射为null或指定的映射包含空键且此映射不允许空键,则抛出此异常。
例子 (Example)
以下示例显示了java.util.TreeMap.putAll()的用法
package com.iowiki;
import java.util.*;
public class TreeMapDemo {
public static void main(String[] args) {
// creating tree maps
TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
TreeMap<Integer, String> treemap_putall = new TreeMap<Integer, String>();
// populating tree map
treemap.put(2, "two");
treemap.put(1, "one");
treemap.put(3, "three");
treemap.put(6, "six");
treemap.put(5, "five");
treemap_putall.put(1, "111");
treemap_putall.put(2, "222");
treemap_putall.put(7, "777");
System.out.println("Value before modification: "+ treemap);
// Putting 2nd map in 1st map
treemap.putAll(treemap_putall);
System.out.println("Value after modification: "+ treemap);
}
}
让我们编译并运行上面的程序,这将产生以下结果。
Value before modification: {1=one, 2=two, 3=three, 5=five, 6=six}
Value after modification: {1=111, 2=222, 3=three, 5=five, 6=six, 7=777}