hashMap提供putAll的方法,可以将另一个对象放入自己的存储空间中,如果有相同的key值则会覆盖之前的key值所对应的value值
public class Test{ public static void main(String [] args){ //HashMap是一种键值对的映射形式,用key和value的形式来保留数据 HashMap<String,String> hashMap1 = new HashMap<String,String>(); hashMap1.put("a1","a"); hashMap1.put("a2","b"); hashMap1.put("a3","c"); //创建一个新的hashMap对象 HashMap<String,String> hashMap2 = new HashMap<String,String>(); hashMap2.put("b1","d"); hashMap2.put("b2","e"); hashMap2.put("a3","f"); hashMap1.putAll(hashMap2); System.out.println(hashMap1); } }
运行结果: