java的深拷贝和浅拷贝,深拷贝在项目中的应用

java的深拷贝和浅拷贝,深拷贝在项目中的应用

浅拷贝

被拷贝对象的所有变量都含有与原来的对象相同的值,如果改被拷贝的对象或者原对象,那么两个对象的值都会修改。

下面代码中,改变student1或者student2 ,两个对象的值就都改变了。

 public static void main(String[] args) throws Exception {
      Student student1 = new Student("小明",12);
      Student student2 = student1;
      student1.setAge(11);
      //student1 和student2 都改变了
      System.out.println(student1+"-----"+student2);
    }
@Data
@AllArgsConstructor
@NoArgsConstructor
class Student {
    private String name;
    private Integer age;
}

深拷贝

深拷贝后的对象与原来的对象是完全隔离的,互不影响,对一个对象的修改并不会影响另一个对象。

项目中,我们常用的就是深拷贝,就是改一个对象,另一个对象不做修改。

深拷贝工具类

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class BeanConvertUtil {

    /**
     * 对象拷贝
     *
     * @param <T> the type parameter
     * @param srcObj the src obj
     * @param destClass the dest class
     *
     * @return the t
     */
    public static <T> T convert(Object srcObj, Class<T> destClass) {
        if (Objects.isNull(srcObj)) {
            return null;
        }
        String srcJson = "";
        if (srcObj instanceof String) {
            srcJson = (String) srcObj;
        } else {
            srcJson = JSON.toJSONString(srcObj);
        }

        return JSON.parseObject(srcJson, destClass);
    }

    /**
     * 对象List拷贝
     *
     * @param <T> the type parameter
     * @param srcObjList the src obj list
     * @param destClass the dest class
     *
     * @return the list
     */
    public static <T> List<T> transformBeanList(List<?> srcObjList, Class<T> destClass) {
        List<T> destList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(srcObjList)) {
            destList = JSON.parseArray(JSON.toJSONString(srcObjList), destClass);
        }

        return destList;
    }

}

深拷贝工具类使用

下面代码中,改变student1对象,student2则不会改变 ,两个对象的值起到了隔离作用。

    public static void main(String[] args) throws Exception {
        
      Student student1 = new Student("小明",12);
      Student student2 = BeanConvertUtil.convert(student1, Student.class);
      student1.setAge(11);
      System.out.println(student1+"----"+student2);

      /**
       * 拷贝List对象
       */
      Student student3 = new Student("小明",12);
      Student student4 = new Student("小明",12);
      List<Student> studentList1 = new ArrayList<>();
      studentList1.add(student3);
      studentList1.add(student4);
      List<Student> studentList2 = BeanConvertUtil.transformBeanList(studentList1, Student.class);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北漂IT民工_程序员_ZG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值