putAll(Map<? extends K,? extends V> map)%uA0方法用于所有从指定映射中的映射关系复制到此映射。这些映射关系将替换此映射的所有当前指定映射中键的所有映射关系。
声明
以下是java.util.TreeMap.putAll()方法的声明。
public void putAll(Map<? extends K,? extends V> map)
参数
-
map--%uA0这是将要存储在此映射的映射。
返回值
NA
异常
-
ClassCastException--%uA0如果类指定映射中的键或值不允许将其存储在此映射抛出此异常。
-
NullPointerException--%uA0如果指定映射为null,或者指定映射包含null键,而此映射不允许null键,将抛出此异常。
例子
下面的例子显示java.util.TreeMap.putAll()方法的使用
package com.yiibai 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}