取两集合的交、并、补、差

该文章介绍了一个名为FilterCompare的Java类,用于处理两个列表的交集、并集、差集和补集,以便于根据前端返回的数据进行数据库的更新、插入和删除操作。通过函数式编程方法,实现了基于特定属性比较的对象列表处理。最后,展示了如何在实际业务中调用这个工具类并更新数据库。
摘要由CSDN通过智能技术生成

业务场景:前端返回的数据,需要对比进行增删改操作

​
public class FilterCompare<T> {
    private final List<T> intersectionList ;
    private final List<T> unionList ;
    private final List<T> differenceList ;
    private final List<T> complenmentList ;

    private FilterCompare(List<T> intersection, List<T> union, List<T> difference, List<T> complenment){
        this.intersectionList = intersection;
        this.unionList = union;
        this.differenceList = difference;
        this.complenmentList = complenment;
    }
    private FilterCompare(){
        this.intersectionList = new ArrayList<>();
        this.unionList = new ArrayList<>();
        this.differenceList = new ArrayList<>();
        this.complenmentList = new ArrayList<>();
    }

    public static <T, R> FilterCompare<T> getInstance(List<T> original, List<T> compare, Function<T, R> fun) {

        if(original.isEmpty() && compare.isEmpty()){
            return new FilterCompare<>();
        }
        List<T> intersection = original.stream().filter(i-> compare.stream().map(fun).collect(Collectors.toList()).contains(fun.apply(i))).collect(Collectors.toList());
        List<T> difference = original.stream().filter(i-> !compare.stream().map(fun).collect(Collectors.toList()).contains(fun.apply(i))).collect(Collectors.toList());
        List<T> complenment = compare.stream().filter(i-> !original.stream().map(fun).collect(Collectors.toList()).contains(fun.apply(i))).collect(Collectors.toList());

        original.addAll(compare);
        return new FilterCompare<>(intersection, original,difference,complenment);
    }

    //交集
    public List<T> getIntersection(){
        return this.intersectionList;
    }
    //并集
    public List<T> getUnion(){
        return this.unionList;
    }
    //差集
    public List<T> getDifference(){
        return this.differenceList;
    }
    //补集
    public List<T> getComplement(){
        return this.complenmentList;
    }

​

调用

       
 FilterCompare<UpBean> filterCompare = FilterCompare.getInstance
                (mobileUpBeanList, UpBeans, UpBean::getId);
        List<UpBean> updateListByMobile = filterCompare.getIntersection();
        List<UpBean>  addListByMobile= filterCompare.getDifference();
        List<UpBean>  delListByMobile= filterCompare.getComplement();

        bridgeUpDao.update(updateListByMobile);
        bridgeUpDao.insert(addListByMobile);
        bridgeUpDao.deleteBy(delListByMobile);
        log.info("[Id:{},app新增:  {}条,修改  {}条,删除{}条,  消耗时间:{}]",
                Id, addListByMobile.size(), updateListByMobile.size(), delListByMobile.size(),
                (System.currentTimeMillis() - st) / 1000.0);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值