栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 拷贝对象集合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 拷贝对象集合

List yoyList = cmcMonthReportMapper.listonthReportInfoDTO(yearOnYearReport);
List 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;


public class SerializationUtils {
    
    @SuppressWarnings("unchecked")
    public static  List deepCopy(List 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) in.readObject();
        } catch (Exception ex) {

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

方法二:使用stream流

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号