标签:lambda 表达式 高级 sys dal string each stream core
业务场景:根据用户查询权限,入参: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") } }
//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")));
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); } }
标签:lambda 表达式 高级 sys dal string each stream core
原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/12861320.html