码迷,mamicode.com
首页 > 其他好文 > 详细

Map.putAll()方法

时间:2017-05-08 10:01:51      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:ace   hashmap   put   sdn   javase   class   get   str   不同   

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那么用后面的覆盖前面的
* */

Map.putAll()方法

标签:ace   hashmap   put   sdn   javase   class   get   str   不同   

原文地址:http://www.cnblogs.com/ceng/p/6823361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!