javaUtil之commons深拷贝

深拷贝和浅拷贝的区别

先简述JVM存储分工
栈:存储基本类型对象(八大数据类型及其包装类)和引用类型对象的地址。
堆:存放引用类型对象的实际值。
String:存在堆中,但是因其不可变性,可以将其看作基本数据类型。

深拷贝就是去堆里面重新复制一个对象出来。
浅拷贝就是去栈里面复制一份基本数据类型的数据及引用数据类型的地址。
深拷贝需要对对象的引用类型属性进行拷贝,才能实现拷贝后原有对象修改的情况下副本对象不会发生改变。
浅拷贝只能保证其对象属性仅为基本变量时不发生改变,引用变量会发生改变。

java实现深拷贝与浅拷贝

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("深拷贝");
        Date date = new Date(0);
        DeepPersion zhangsan = new DeepPersion(1, "zhangsan", date);
        DeepPersion clone = (DeepPersion) zhangsan.clone();
        zhangsan.setId(2);
        zhangsan.setName("lisi");
        date.setTime(System.currentTimeMillis());
        zhangsan.setBirthday(new Date(System.currentTimeMillis()));
        System.out.println(clone);
        System.out.println("浅拷贝");
        date = new Date(0);
        UnDeepPersion zhangsan1 = new UnDeepPersion(1, "zhangsan", date);
        UnDeepPersion unDeepPersion = (UnDeepPersion) zhangsan1.clone();
        zhangsan1.setId(2);
        zhangsan1.setName("lisi");
        date.setTime(System.currentTimeMillis()-1000*60*60*24);
        zhangsan1.setBirthday(date);
        System.out.println(unDeepPersion);
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class DeepPersion implements Cloneable{
    private Integer id;
    private String name;
    private Date birthday;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        DeepPersion deepPersion = (DeepPersion) super.clone();
        deepPersion.birthday = (Date) this.birthday.clone();
        return deepPersion;
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class UnDeepPersion implements Cloneable{
    private Integer id;
    private String name;
    private Date birthday;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

深拷贝
DeepPersion(id=1, name=zhangsan, birthday=Thu Jan 01 08:00:00 CST 1970)
浅拷贝
UnDeepPersion(id=1, name=zhangsan, birthday=Thu Aug 26 10:45:19 CST 2021)

使用commons-lang3实现深拷贝

使用commons-lang3实现深拷贝时实体类需实现Serializable接口,并通过SerializationUtils.clone(T)进行拷贝。

		<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
public class CloneTest {
    public static void main(String[] args) {
        System.out.println("深拷贝");
        Date date = new Date(0);
        DeepPersion zhangsan = new DeepPersion(1, "zhangsan", date);
        DeepPersion clone = SerializationUtils.clone(zhangsan);
        zhangsan.setId(2);
        zhangsan.setName("lisi");
        date.setTime(System.currentTimeMillis());
        zhangsan.setBirthday(new Date(System.currentTimeMillis()));
        System.out.println(clone);
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class DeepPersion implements Serializable {
    private static final long serialVersionUID = 7102882201703761731L;
    private Integer id;
    private String name;
    private Date birthday;
}

深拷贝
DeepPersion(id=1, name=zhangsan, birthday=Thu Jan 01 08:00:00 CST 1970)

对象使用=复制

使用=直接赋值的时候,两者传递的时对象的地址,对象本身没有发生复制操作,所以对旧对象的操作,也会引起新对象的变化:

public class CloneTest {
    public static void main(String[] args) {
        System.out.println("=直接赋值");
        Date date = new Date(0);
        DeepPersion zhangsan = new DeepPersion(1, "zhangsan", date);
        DeepPersion clone =zhangsan;
        zhangsan.setId(2);
        zhangsan.setName("lisi");
        date.setTime(System.currentTimeMillis());
        zhangsan.setBirthday(new Date(System.currentTimeMillis()));
        System.out.println(clone);
    }
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class DeepPersion implements Serializable {
    private static final long serialVersionUID = 7102882201703761731L;
    private Integer id;
    private String name;
    private Date birthday;
}

DeepPersion(id=2, name=lisi, birthday=Fri Aug 27 10:57:12 CST 2021)

BeanUtils.copyProperties

Spring中的BeanUtils.copyProperties能实现对对象的拷贝,使得我们在拼装一些VO时不需要对字段逐个set,直接使用BeanUtils.copyProperties即可:

public class BeanCpoyTest {
    public static void main(String[] args) {
        Person person = new Person(1L,"zhangsan");
        Person1 person1 = new Person1();
        BeanUtils.copyProperties(person,person1);
        System.out.println(person1);
    }
}

@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@TableName("t_person")
class Person {
    private Long id;
    private String username;
}
@SuperBuilder
@Data
@NoArgsConstructor
class Person1 {
    private Long id;
    private String username;
    private String passwd;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值