Map.putAll()用法

来源:互联网 发布:君子去仁 恶乎成名翻译 编辑:程序博客网 时间:2024/06/01 10:30

Map.putAll()用法

标签: hashmapimportstringclassc
 22886人阅读 评论(0) 收藏 举报
 分类:
 
 

import java.util.HashMap;

public class Map_putAllTest {
public static void main(String[] args){
   //两个map具有不同的key
   HashMap map1=new HashMap(); 
   map1.put("1", "A"); 
   HashMap map2 = new HashMap(); 
   map2.put("2", "B"); 
   map2.put("3", "C"); 
   map1.putAll(map2); 
   System.out.println(map1);
   //两个map具有重复的key
   HashMap map3=new HashMap(); 
   map3.put("1", "A"); 
   HashMap map4 = new HashMap(); 
   map4.put("1", "B"); 
   map4.put("3", "C"); 
   map3.putAll(map4); 
   System.out.println(map3);
}
}
/* 输出结果:
* {3=C, 2=B, 1=A}
* {3=C, 1=B}
* 结论:putAll可以合并两个MAP,只不过如果有相同的key那么用后面的覆盖前面的
* */

0 0