public void mapadd(){
Map<String, Integer > map1;
map1 = new HashMap<String, Integer >();
map1.put("jack",20);
map1.put("tom",11);
map1.put("paul",30);
System.out.println("map1 = " + map1);
Map<String,Integer> map2;
map2 = new HashMap<String,Integer>();
map2.put("lisi",30);
map2.put("zhangsan",22);
System.out.println("map2 = " + map2);
map1.putAll(map2);
System.out.println("map1=" + map1);
System.out.println("=================================");
System.out.println("Jack's age is :" + map1.get("jack"));
System.out.println("=================================");
System.out.println("contain true:"+ map1.containsKey("tom"));
System.out.println("contain false:"+ map1.containsValue("dan"));
System.out.println("is empty false:"+ map1.isEmpty());
System.out.println("=================================");
for(String key : map1.keySet()){
Integer keyvalue = map1.get(key);
System.out.println(""+key + ":"+ keyvalue);
}
}
输出结果:
map1 = {tom=11, paul=30, jack=20}
map2 = {lisi=30, zhangsan=22}
map1={lisi=30, tom=11, zhangsan=22, paul=30, jack=20}
=================================
Jack's age is :20
=================================
contain true:true
contain false:false
is empty false:false
=================================
lisi:30
tom:11
zhangsan:22
paul:30
jack:20