JAVA利用序列化和反序列化的方式进行深拷贝

博客指出使用Spring的BeanUtils.copyProperties()进行对象赋值时,对复合对象只能拷贝引用,可能导致运算问题。由于Java本身不支持深拷贝,可利用序列化和反序列化方式实现深拷贝,同时提醒实体类需实现Serializable接口。

在使用spring 的 BeanUtils.copyProperties()进行对象赋值时,只能拷贝单一字段的值,如果对象是复合对象,比如成员变量中有对象,链表等时,BeanUtils.copyProperties()拷贝的还是复杂成员变量的引用,如下代码:

List<Particle> particleList = new ArrayList<>();
        for (Particle particle : particles.getParticles()) {
            Particle temp = new Particle();
            BeanUtils.copyProperties(particle, temp);
            //Particle temp = CommonUtil.deepCloneObject(particle);
            particleList.add(temp);
        }

 用BeanUtils.copyProperties()将particle拷贝到temp时,由于path是一个链表,此时并不会拷贝值,而是一个引用,本质上path还是一个对象,这在运算中可能是致命的。

而java本身是不支持深拷贝的,可以 利用序列化和反序列化的方式进行深拷贝(致谢这位大佬),方法直接拿过去用,代码如下:

 /**
     * 对象的深度克隆,此处的对象涉及Collection接口和Map接口下对象的深度克隆
     * 利用序列化和反序列化的方式进行深度克隆对象
     *
     * @param <T> 待克隆对象的数据类型
     * @param object 待克隆的对象
     * @return 已经深度克隆过的对象
     */
    public static <T extends Serializable> T deepCloneObject(Particle object) {
        T deepClone = null;
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            bais = new ByteArrayInputStream(baos
                    .toByteArray());
            ois = new ObjectInputStream(bais);
            deepClone = (T)ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if(oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try{
                if(bais != null) {
                    bais.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try{
                if(ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return deepClone;
    }

使用一下:

List<Particle> particleList = new ArrayList<>();
        for (Particle particle : particles.getParticles()) {
            Particle temp = CommonUtil.deepCloneObject(particle);
            particleList.add(temp);
        }

 可以看到path是一个新对象:

注意事项,实体类要 implements Serializable接口,比如我用的实体是Particle:

@Data
@Repository
@AllArgsConstructor
@NoArgsConstructor
public class Particle implements Serializable {

    private double lon; //经度
    private double lat; //纬度
    private int level; //楼层
    private int orientation; //方向
    private float weight = 1; //权重
    private List<Point> path = new ArrayList<>(); //粒子走过的路径

    private int test = 1; //测试字段
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值