Map深度复制
目录
如何对Map类型的数据进行深度复制,有几种方式呢?
1,new HashMap<>(Object);
代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
testNewMap();
}
public static void testNewMap(){
System.out.println("====== new HashMap<>(e) =======");
Map<String, Object> one = new HashMap<>();
one.put("one","fish");
Map<String, Object> two = new HashMap<>(one);
System.out.println("init: one: "+ one.toString());
System.out.println("init: two: "+ two.toString());
System.out.println("====== change to grass =======");
two.put("one","grass");
System.out.println("change: one: "+ one.toString());
System.out.println("change: two: "+ two.toString());
System.out.println();
}
使用 new HashMap<>(one) 的方式
结果:
====== new HashMap<>(e) =======
init: one: {one=fish}
init: two: {one=fish}
====== change to grass =======
change: one: {one=fish}
change: two: {one=grass}
2,Put All;
代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
testPutAllMap();
}
public static void testPutAllMap(){
System.out.println("====== put all map =======");
Map<String, Object> one = new HashMap<>();
one.put("one","fish");
Map<String, Object> two = new HashMap<>();
two.putAll(one);
System.out.println("init: one: "+ one.toString());
System.out.println("init: two: "+ two.toString());
System.out.println("====== change to monkey =======");
two.put("one","monkey");
System.out.println("change: one: "+ one.toString());
System.out.println("change: two: "+ two.toString());
System.out.println();
}
结果:
====== put all map =======
init: one: {one=fish}
init: two: {one=fish}
====== change to monkey =======
change: one: {one=fish}
change: two: {one=monkey}
这边会标黄,处理后会变成第一种的情况。
3,串行化复制;
代码:
public static void main(String[] args) throws IOException, ClassNotFoundException {
testDeepCopyMap();
}
public static void testDeepCopyMap() throws IOException, ClassNotFoundException {
System.out.println("====== DeepCopy Map =======");
Map<String, Object> one = new HashMap<>();
one.put("one","fish");
Map<String, Object> two = CloneUtils.cloneMap(one);;
System.out.println("init: one: "+ one.toString());
System.out.println("init: two: "+ two.toString());
System.out.println("====== change to snake =======");
two.put("one","snake");
System.out.println("change: one: "+ one.toString());
System.out.println("change: two: "+ two.toString());
System.out.println();
}
串行化代码:
public class CloneUtils {
public static <T extends Serializable> T clone(T src) throws IOException, ClassNotFoundException {
ObjectInputStream ois = getInputStream(src);
//返回生成的新对象
@SuppressWarnings("unchecked")
T cloneObj = (T) ois.readObject();
ois.close();
return cloneObj;
}
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
ObjectInputStream ois = getInputStream(src);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) ois.readObject();
ois.close();
return dest;
}
public static <K,V> Map<K,V> cloneMap(Map<K,V> src) throws IOException, ClassNotFoundException {
ObjectInputStream ois = getInputStream(src);
@SuppressWarnings("unchecked")
Map<K, V> result = (Map<K, V>) ois.readObject();
ois.close();
return result;
}
private static ObjectInputStream getInputStream(Object src) throws IOException, ClassNotFoundException {
//写入字节流
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream obs = new ObjectOutputStream(out);
obs.writeObject(src);
obs.close();
//分配内存,写入原始对象,生成新对象
ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());
return new ObjectInputStream(ios);
}
}
结果:
====== DeepCopy Map =======
init: one: {one=fish}
init: two: {one=fish}
====== change to snake =======
change: one: {one=fish}
change: two: {one=snake}
总结:
在使用Map复制的时候,一般用第一种的方式,如果大量使用,也可以考虑用第三种方式。