1. 程式人生 > >Map.putAll()用法

Map.putAll()用法

輸出結果 net pla rgs get system void util ace

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()用法