java中深复制知识点详解
小妮浅浅 人气:0在本篇文章里小编给大家整理了关于java中深复制知识点详解内容,有需要的朋友们可以学习下。
在正式开始深复制的讲解之前,我们先来理解一下概念。假设一个物品需要批量生产,但是这个物品还配有赠品,生产的时候需要把赠品也列在计划内。所谓深复制的原理就是这样,我们不能只复制属性,包括引用之类的附带也需要被复制。下面小编就为大家带来深复制的两种不同方法。
1.序列化实现
如下为谷歌Gson序列化HashMap,实现深度复制的例子:
public class CopyDeepMapTest { public static void main(String[] args) { HashMap<Integer, User> userMap = new HashMap<>(); userMap.put(1, new User("jay", 26)); userMap.put(2, new User("fany", 25)); //Shallow clone Gson gson = new Gson(); String jsonString = gson.toJson(userMap); Type type = new TypeToken<HashMap<Integer, User>>(){}.getType(); HashMap<Integer, User> clonedMap = gson.fromJson(jsonString, type); //Same as userMap System.out.println(clonedMap); System.out.println("\nChanges DO NOT reflect in other map \n"); //Change a value is clonedMap clonedMap.get(1).setName("test"); //Verify content of both maps System.out.println(userMap); System.out.println(clonedMap); } }
运行结果:
{1=User{name='jay', age=26}, 2=User{name='fany', age=25}} Changes DO NOT reflect in other map {1=User{name='jay', age=26}, 2=User{name='fany', age=25}} {1=User{name='test', age=26}, 2=User{name='fany', age=25}}
从运行结果看出,对cloneMap修改,userMap没有被改变,所以是深度复制。
2.list深复制
两个list数据相同但是地址值不同,A和B是独立的两个list,A改变了B不会改变,B改变了A也不会改变
深复制的工具类方法:
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); @SuppressWarnings("unchecked") List<T> dest = (List<T>) in.readObject(); return dest; }
关于深拷贝的一个扩展实例:
public class YuelyLog implements Serializable,Cloneable { private Attachment attachment; private String name; private String date; @Override protected YuelyLog clone() throws CloneNotSupportedException { return (YuelyLog)super.clone(); } public Attachment getAttachment() { return attachment; } public void setAttachment(Attachment attachment) { this.attachment = attachment; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } /** * 使用序列化技术实现深拷贝 * @return */ public YuelyLog deepClone() throws IOException,ClassNotFoundException{ //将对象写入流中 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(this); //从流中取出 ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); return (YuelyLog)objectInputStream.readObject(); } }
加载全部内容