Java TreeMap putAll()方法
java.util.TreeMap.putAll() 用于将所有键值对从一个Map复制到另一个Map。
1 语法
public void putAll(Map<? extends K,? extends V> map)
2 参数
map:这是要存储在此映射中的映射。
3 返回值
无
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.TreeMap.putAll()方法的例子
*/
import java.util.*;
public class Demo {
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}
热门文章
优秀文章