JAVA几种方式实现深拷贝
短信预约 -IT技能 免费直播动态提醒
JAVA几种方式实现深拷贝
准备
定义两个类用于测试拷贝,类内容如下,目的是深拷贝一个User类的对象:
@Data@Accessors(chain = true)public class User { private Integer id; private Integer age; private String name; private Car car; private String category;}
@Data@Accessors(chain = true)public class Car { private Integer id; private String color; private String name;}
实现
package com.demo;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import lombok.Data;import lombok.experimental.Accessors;import java.io.*;@Data@Accessors(chain = true)public class User implements Cloneable, Serializable { private Integer id; private Integer age; private String name; private Car car; private String category; @Override public User clone() throws CloneNotSupportedException { return (User) super.clone(); } public User copyUser1() { User copyUser = new User() .setId(this.getId()) .setName(this.getName()) .setAge(this.getAge()) .setCategory(this.getCategory()); if (this.getCar() != null) { copyUser.setCar(new Car().setId(this.getCar().getId()) .setColor(this.getCar().getColor()) .setName(this.getCar().getName())); } return copyUser; } public User copyUser2() throws CloneNotSupportedException { User cloneUser = this.clone(); if(this.getCar() != null) { cloneUser.setCar(this.getCar().clone()); } return cloneUser; } public User copyUser3() throws IOException, ClassNotFoundException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (User) ois.readObject(); } public User copyUser4() throws JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(objectMapper.writeValueAsString(this),User.class); }}
验证
package com.demo;import java.io.IOException;public class CopyDemo { public static void main(String[] args) throws IOException, CloneNotSupportedException, ClassNotFoundException { User user = new User().setAge(10).setName("李四").setId(3).setCategory("工人"); user.setCar(new Car().setName("保时捷").setId(999).setColor("黑色")); User copyUser1 = user.copyUser1(); System.out.println("copyUser1:" + copyUser1); System.out.println("copyUser1与user对象是否是同一个:" + (System.identityHashCode(user) == System.identityHashCode(copyUser1))); System.out.println("copyUser1中的car与user中的car是否是同一个:"+(System.identityHashCode(user.getCar()) == System.identityHashCode(copyUser1.getCar()))); System.out.println("===================="); User copyUser2 = user.copyUser2(); System.out.println("copyUser2:" + copyUser2); System.out.println("copyUser2与user对象是否是同一个:" + (System.identityHashCode(user) == System.identityHashCode(copyUser2))); System.out.println("copyUser2中的car与user中的car是否是同一个:"+(System.identityHashCode(user.getCar()) == System.identityHashCode(copyUser2.getCar()))); System.out.println("===================="); User copyUser3 = user.copyUser3(); System.out.println("copyUser3:" + copyUser3); System.out.println("copyUser3与user对象是否是同一个:" + (System.identityHashCode(user) == System.identityHashCode(copyUser3))); System.out.println("copyUser3中的car与user中的car是否是同一个:"+(System.identityHashCode(user.getCar()) == System.identityHashCode(copyUser3.getCar()))); System.out.println("===================="); User copyUser4 = user.copyUser4(); System.out.println("copyUser4:" + copyUser4); System.out.println("copyUser4与user对象是否是同一个:" + (System.identityHashCode(user) == System.identityHashCode(copyUser4))); System.out.println("copyUser4中的car与user中的car是否是同一个:"+(System.identityHashCode(user.getCar()) == System.identityHashCode(copyUser4.getCar()))); }}
验证结果
copyUser1:User(id=3, age=10, name=李四, car=Car(id=999, color=黑色, name=保时捷), category=工人)copyUser1与user对象是否是同一个:falsecopyUser1中的car与user中的car是否是同一个:false====================copyUser2:User(id=3, age=10, name=李四, car=Car(id=999, color=黑色, name=保时捷), category=工人)copyUser2与user对象是否是同一个:falsecopyUser2中的car与user中的car是否是同一个:false====================copyUser3:User(id=3, age=10, name=李四, car=Car(id=999, color=黑色, name=保时捷), category=工人)copyUser3与user对象是否是同一个:falsecopyUser3中的car与user中的car是否是同一个:false====================copyUser4:User(id=3, age=10, name=李四, car=Car(id=999, color=黑色, name=保时捷), category=工人)copyUser4与user对象是否是同一个:falsecopyUser4中的car与user中的car是否是同一个:false
结论
使用java原生推荐方法三,方法一、方法二缺点过于明显,第三方库的方式可以用方法四,spring boot默认的序列化反序列化就是Jackson,另外比照方法四同类的类库也能实现
来源地址:https://blog.csdn.net/qq_43842093/article/details/132137006
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341