Java实现Map深拷贝
作者:互联网
开题
Map进行复制时候,会涉及到两种场景
1、map中不包含对象
2、map中包含对象
1、map中不包含对象
@Test
public void putAllTest1() throws InterruptedException {
Map<String, Object> goods = new LinkedHashMap<>();
goods.put("name", "可口可乐");
goods.put("price", 3);
System.out.println("可乐原本价格:" + JSON.toJSONString(goods));
Map<String, Object> synGoods = new LinkedHashMap<>();
synGoods.putAll(goods);
//可口可乐涨价了
synGoods.put("price", 4);
System.out.println("拟涨价后应售价:" + JSON.toJSONString(synGoods));
System.out.println("拟涨价后原售价:" + JSON.toJSONString(goods));
}
运行结果:
可乐原本价格:{"name":"可口可乐","price":3}
拟涨价后应售价:{"name":"可口可乐","price":4}
拟涨价后原售价:{"name":"可口可乐","price":3}
结果与预期一致 ,可乐拟涨价还没发布,原本价格保持不变。
2、map中包含对象
@Test
public void putAllTest2() throws InterruptedException {
Map<String, Object> goods = new LinkedHashMap<>();
goods.put("name", "可口可乐");
goods.put("price", 3);
Map<String, Object> childGoods = new HashMap<>();
childGoods.put("name", "百事可乐");
childGoods.put("price", 3);
goods.put("childGoods", childGoods);
System.out.println("可乐原本价格:" + JSON.toJSONString(goods));
Map<String, Object> synGoods = new LinkedHashMap<>();
synGoods.putAll(goods);
//可口可乐涨价了
synGoods.put("price", 4);
Map<String, Object> synChildGoods = (Map<String, Object>) synGoods.get("childGoods");
//百事可乐见势一起涨价
synChildGoods.put("price", 4);
System.out.println("拟涨价后应售价:" + JSON.toJSONString(synGoods));
System.out.println("拟涨价后原售价:" + JSON.toJSONString(goods));
}
运行结果
可乐原本价格:{"name":"可口可乐","price":3,"childGoods":{"price":3,"name":"百事可乐"}}
拟涨价后应售价:{"name":"可口可乐","price":4,"childGoods":{"price":4,"name":"百事可乐"}}
拟涨价后原售价:{"name":"可口可乐","price":3,"childGoods":{"price":4,"name":"百事可乐"}}
百事可乐见可口可乐涨价,也要跟着涨价 ,并且作为可口可乐的子产品进行涨价。
从结果中我们可以发现,可口可乐拟涨价后,原价格保持不变,但是子产品百事可乐价格在拟涨价后,原价格也跟着变了,这不是我们想要的结果。
这说明,同步信息封装的时候修改了子产品的信息,这并不是我们想要的结果。这里其实就涉及到对象的浅拷贝和深拷贝的问题。
如何实现map的深拷贝?
序列化的方式可以实现对象的深拷贝,但是对象必须是实现了Serializable接口才可以,Map本身没有实现 Serializable 这个接口,不能实现深拷贝,但是HashMap实现了Serializable,可以进行深拷贝。
首先,附上深拷贝的方法
/**
* 使用对象的序列化进而实现深拷贝
* @param obj
* @param <T>
* @return
*/
private <T extends Serializable> T clone(T obj) {
T cloneObj = null;
try {
ByteOutputStream bos = new ByteOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
cloneObj = (T) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
return cloneObj;
}
然后我们对刚刚的方法稍微进行修改
@Test
public void cloneTest() throws InterruptedException {
//Map改为HashMap
HashMap<String, Object> goods = new LinkedHashMap<>();
goods.put("name", "可口可乐");
goods.put("price", 3);
Map<String, Object> childGoods = new HashMap<>();
childGoods.put("name", "百事可乐");
childGoods.put("price", 3);
goods.put("childGoods", childGoods);
System.out.println("可乐原本价格:" + JSON.toJSONString(goods));
Map<String, Object> synGoods = new LinkedHashMap<>();
//putAll改为clone实现深度拷贝
synGoods = clone(goods);
//可口可乐涨价了
synGoods.put("price", 4);
Map<String, Object> synChildGoods = (Map<String, Object>) synGoods.get("childGoods");
//百事可乐见势一起涨价
synChildGoods.put("price", 4);
System.out.println("拟涨价后应售价:" + JSON.toJSONString(synGoods));
System.out.println("拟涨价后原售价:" + JSON.toJSONString(goods));
}
输出结果
可乐原本价格:{"name":"可口可乐","price":3,"childGoods":{"price":3,"name":"百事可乐"}}
拟涨价后应售价:{"name":"可口可乐","price":4,"childGoods":{"price":4,"name":"百事可乐"}}
拟涨价后原售价:{"name":"可口可乐","price":3,"childGoods":{"price":3,"name":"百事可乐"}}
从结果中可以发现,两种可乐原价格在拟涨价后保持不变,完成深度拷贝!
结论:可口可乐真香!!!!
标签:Map,goods,Java,name,childGoods,price,put,拷贝,可口可乐 来源: https://blog.csdn.net/qinghuidu/article/details/116738609