java 拷贝对象集合

List<MonthReportInfoDTO> yoyList = cmcMonthReportMapper.listonthReportInfoDTO(yearOnYearReport);
List<MonthReportInfoDTO> yoyList01 = new ArrayList<>();

想拷贝 yoyList 到yoyList01,

 {
                "id": 245,
                "reportId": 253,
                "industryId": 2,
                "subjectVersionId": "2203157YR1WBTGXP",
                "fillingMode": 2,
                "status": 1,
                "gmtCreate": "2022-06-08 14:27:31",
                "gmtModified": "2022-06-09 22:29:23",
                "listFacilityIndustryRela": [
                    {
                        "id": 266,
                        "reportIndustryId": 245,
                        "facilitiesCode": "220606DXX2C7CZC0",
                        "facilitiesName": "机组23",
                        "status": 1,
                        "gmtCreate": "2022-06-08 14:27:57",
                        "gmtModified": "2022-06-08 14:27:57"
                    }
                ]
            },
            {
                "id": 303,
                "reportId": 253,
                "industryId": 3,
                "subjectVersionId": "2111237926GADM14",
                "fillingMode": 2,
                "status": 1,
                "gmtCreate": "2022-06-08 14:27:31",
                "gmtModified": "2022-06-08 14:27:31",
                "listFacilityIndustryRela": [
                    {
                        "id": 303,
                        "reportIndustryId": 303,
                        "facilitiesCode": "220606DXX2C7CZC0",
                        "facilitiesName": "机组23",
                        "status": 1,
                        "gmtCreate": "2022-06-08 14:27:57",
                        "gmtModified": "2022-06-08 14:27:57"
                    },
                    {
                        "id": 304,
                        "reportIndustryId": 303,
                        "facilitiesCode": "220606DXX2C7CZC0",
                        "facilitiesName": "机组24",
                        "status": 1,
                        "gmtCreate": "2022-06-10 17:56:41",
                        "gmtModified": "2022-06-10 17:56:41"
                    }
                ]
            }

然后想删除yoyList01里所有的listFacilityIndustryRela,不影响其他数据,

for (MonthReportInfoDTO yoy: yoyList01) {
            yoy.getListFacilityIndustryRela().clear();
}

然后发现

yoyList01.addAll(yoyList)
和循环
for (MonthReportInfoDTO yoy: yoyList) {
    yoyList01.add(yoy);
}

都是浅拷贝,对原数据都有影响

有两种解决方法

方法一:

调用工具类

      yoyList01 = SerializationUtils.deepCopy(yoyList);
package com.sinocarbon.carbondata.utils;

import com.baomidou.mybatisplus.toolkit.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.List;

/**
 * @author chenzhigang
 * @apiNote
 * @since 2022/6/10
 */
public class SerializationUtils {
    /**
     * 深拷贝
     *
     * @param src 原始数据
     * @param <T> 泛型
     * @return 深拷贝数据
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> deepCopy(List<T> src) {
        ByteArrayOutputStream byteOut = null;
        ObjectOutputStream out = null;
        ByteArrayInputStream byteIn = null;
        ObjectInputStream in = null;
        try {
            byteOut = new ByteArrayOutputStream();
            out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            in = new ObjectInputStream(byteIn);
            return (List<T>) in.readObject();
        } catch (Exception ex) {

        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(byteIn);
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(byteOut);
        }
        return Collections.emptyList();
    }
}

方法二:使用stream流

 List<MonthReportInfoDTO> collect = yoyList.stream().map( items -> {
            MonthReportInfoDTO dto = new MonthReportInfoDTO();
            dto.setListFacilityIndustryRela(null);
            dto.setFillingMode(items.getFillingMode());
            ....
            return dto;
 }).collect(Collectors.toList());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值