sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

两个List集合取交集、并集、差集

业务场景:根据用户查询权限,入参:UserCode,lastQueryTime(上次查询时间),出参:权限变化的列表。
oldList(上次查询到的权限列表),currList(当前查询到的权限列表),比对两个list找出:移除和增加的权限加以标识(1--增加,-1--删除)返回。
原逻辑处理方法:循环currList,如果oldList中不包含即为增加的权限列表,反之,循环oldList,如果currList中不包含即为移除的权限列表,代码如下(简略写没运行,比较笨拙):

//之前写的已经删了手动打下(add处有问题应该用map,将就着看吧)
List<String> removeList =new ArrayList<String>();
List<String> addList =new ArrayList<String>();
for(String old:currList){
    if(!currList.contains(old)){
        removeList.add(old,"-1")
    }
}
for(String curr:oldList){
    if(!oldList.contains(curr)){
        addList.add(curr,"1")
    }
}

用Java8中的 lambda表达式处理:

//AppAuths返回的变化列表
// 移除权限:上次与当前的权限取差集 (oldAppPrivileges - currAppPrivileges)
List<AppPrivilege> removePrivileges = oldAppPrivileges.stream()
.filter(oldPrivilege ->!currAppPrivileges.contains(oldPrivilege)).collect(toList());
removePrivileges.parallelStream().forEachOrdered(removePrivilege -> 
appAuths.add(new AppAuths(removePrivilege, "-1")));
 
// 增加权限:当前与上次的权限取差集 (currAppPrivileges - oldAppPrivileges)
List<AppPrivilege> addPrivileges = currAppPrivileges.stream()
.filter(currPrivilege ->!oldAppPrivileges.contains(currPrivilege)).collect(toList());
addPrivileges.parallelStream().forEachOrdered(addPrivilege -> 
appAuths.add(new AppAuths(addPrivilege, "1")));

逻辑其实是一样的,但下面的代码会给人一种高级的感觉(个人认为),性能方面下面的要好很多。
下面是:两个List集合取交集、并集、差集、去重并集的一个简单Demo,可供参考:

package com.ymdd.galaxy.appmanage.core.appauth.service;
 
import java.util.ArrayList;
import java.util.List;
import static java.util.stream.Collectors.toList;
 
 
public class Test {
 
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("5");
        list1.add("6");
 
        List<String> list2 = new ArrayList<String>();
        list2.add("2");
        list2.add("3");
        list2.add("7");
        list2.add("8");
 
        // 交集
        List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
        System.out.println("---交集 intersection---");
        intersection.parallelStream().forEach(System.out :: println);
 
        // 差集 (list1 - list2)
        List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
        System.out.println("---差集 reduce1 (list1 - list2)---");
        reduce1.parallelStream().forEach(System.out :: println);
 
        // 差集 (list2 - list1)
        List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
        System.out.println("---差集 reduce2 (list2 - list1)---");
        reduce2.parallelStream().forEach(System.out :: println);
 
        // 并集
        List<String> listAll = list1.parallelStream().collect(toList());
        List<String> listAll2 = list2.parallelStream().collect(toList());
        listAll.addAll(listAll2);
        System.out.println("---并集 listAll---");
        listAll.parallelStream().forEachOrdered(System.out :: println);
 
        // 去重并集
        List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
        System.out.println("---得到去重并集 listAllDistinct---");
        listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
 
        System.out.println("---原来的List1---");
        list1.parallelStream().forEachOrdered(System.out :: println);
        System.out.println("---原来的List2---");
        list2.parallelStream().forEachOrdered(System.out :: println);
 
    }
}

————————————————
本文转载自CSDN博客
原文链接:https://blog.csdn.net/weixin_38256991/article/details/81672235

https://blog.csdn.net/qq_26502245/article/details/108510131?utm_medium=distribute.wap_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1.wap_blog_relevant_default&spm=1001.2101.3001.4242.2
posted on   sunny123456  阅读(471)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示