使用CollUtil工具类快速实现List集合的数据更新

最近研究hutool.core.collection.CollUtil类正好最近项目在用分享一下代码把

主要实现
//创建一个对象
@Data
@AllArgsConstructor
public class User {
    private String code;
    private String name;
    private int age;
}
的就是A集合里的数据和B集合里的数据进行一个数据更新直接看代码把
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.log4j.Log4j2;

import java.util.*;
import java.util.stream.Collectors;

@Log4j2
public class Text {
    /**
     * 1.返回需要更新的数据
     * 2.返回需要删除的数据
     * 3.返回需要新增的数据
     *
     * @param args
     */
    public static void main(String[] args) {
        //旧数据
        List<User> oldList = new java.util.ArrayList<>(List.of(
                new User("001", "张飞", 18),
                new User("002", "李四", 18),
                new User("003", "王五", 18),
                new User("004", "刘备", 18),
                new User("005", "郭七", 18)

        ));
//        List<User> oldList = new java.util.ArrayList<>();//测试空的集合
//        List<User> newList = new java.util.ArrayList<>();
        //需要更新的新数据
        List<User> newList = List.of(
                new User("001", "张费", 18),
                new User("002", "李四", 19),
                new User("003", "王五", 20),
                new User("004", "刘备", 18),
                new User("007", "老八", 18)
        );
        Set<String> newCodes = newList.stream().map(User::getCode).collect(Collectors.toSet());
        Set<String> oldCodes = oldList.stream().map(User::getCode).collect(Collectors.toSet());
        //删除的数据
        oldList.stream().filter(x -> CollUtil.subtract(oldCodes, newCodes).contains(x.getCode())).collect(Collectors.toList()).forEach(x-> oldList.removeIf(c -> c.equals(x)));
        //添加的数据
        oldList.addAll(newList.stream().filter(x -> CollUtil.subtract(newCodes, oldCodes).contains(x.getCode())).collect(Collectors.toList()));
        //更新的数据
        List<User> update = newList.stream().filter(x -> CollUtil.intersectionDistinct(newCodes, oldCodes).contains(x.getCode())).collect(Collectors.toList());
        update.forEach(x->{
            User collect = oldList.stream().filter(c -> c.getCode().equals(x.getCode())).collect(Collectors.toList()).get(0);
            if (!ObjectUtil.equals(collect,x)) {
                //覆盖数据
                oldList.set(oldList.indexOf(collect), x);
                log.warn("更新的数据有:{}",x);
            }
        });
        //Code排序
        oldList.sort(Comparator.comparing(User::getCode));
        oldList.forEach(System.out::println);
    }
}

 最终旧的oldList里的数据就是比对后数据

1.删除旧集合中没有新集合中有的数据

2.添加旧集合中没有新集合中有的数据

3.更新对比新旧集合都有的数据,name和age没有变动就跳过,如果有就直接set覆盖

更新的数据有:User(code=001, name=张费, age=18)
更新的数据有:User(code=002, name=李四, age=19)
更新的数据有:User(code=003, name=王五, age=20)

User(code=001, name=张费, age=18)
User(code=002, name=李四, age=19)
User(code=003, name=王五, age=20)
User(code=004, name=刘备, age=18)
User(code=007, name=老八, age=18)

CollUtil是一个集合工具类,它提供了一些方便的方法来操作和处理集合。引用中给出了join方法的使用示例,它可以将一个集合中的元素用指定的分隔符连接成一个字符串。例如,给定一个字符串数组["a","b","c","d","e"],可以使用CollUtil.newArrayList方法将其转换为List集合,然后使用CollUtil.join方法将集合中的元素用"#"分隔符连接起来,得到"a#b#c#d#e"的字符串。 除了join方法,CollUtil还提供了其他一些方法来创建和操作集合。引用中给出了newArrayList方法的使用示例,它可以创建一个新的ArrayList并填充指定的元素。例如,可以使用CollUtil.newArrayList方法创建一个新的空列表List<String> list1 = CollUtil.newArrayList(),或者通过传递元素来创建一个包含指定元素的列表List<Integer> list1 = CollUtil.newArrayList(1, 2, 3)。 总的来说,CollUtil是一个方便的集合工具类,可以帮助开发人员更轻松地进行集合操作和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Hutool核心工具类集合工具-CollUtil](https://blog.csdn.net/qq_40579139/article/details/107938199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Hutool之集合工具——CollectionUtil](https://blog.csdn.net/weixin_34179762/article/details/91687526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值