前言:  lambda是一个匿名函数  ()里的表示参数  {}里的表示方法体  -> 表示lambda运算符常用的表达式如下:一、groupingBy用法目的:数据库(想把相同的合并到一起)代码示例://1.分页查询,通过重点任务主题、重点事项、重点负责人或任务的开始时间、结束时间List importantTaskListDTOList = importantTaskMapper.getList(userId, taskDO, importantTaskSearchDTO);//2.找每个重点任务对应的单个或多个负责人//生成一个map,键是按照groupingBy的分组编号,值是对应的分组list集合Map<Long, List> directorListMap = importantTaskListDTOList.stream().collect(Collectors.groupingBy(ImportantTaskListDTO::getTaskId));二、foreach用法//生成一个map,键是按照groupingBy的分组编号,值是对应的分组list集合Map<Long, List> directorListMap = importantTaskListDTOList.stream().collect(Collectors.groupingBy(ImportantTaskListDTO::getTaskId));logger.info(“directorListMap=”+directorListMap);importantTaskListDTOList.forEach(v1 -> { Long taskId = v1.getTaskId(); List directorList = directorListMap.get(taskId); StringBuilder directorNamesBuilder = new StringBuilder(); directorList.forEach(v2 -> { Long directorUserId = v2.getDirectorUserId(); String directorUserName = v2.getDirectorUserName(); directorNamesBuilder.append(directorUserName).append(“,”); }); String directorNames = StringBuilderUtil.removeCharacterOfEndOfMessage(directorNamesBuilder); //负责人名称们(逗号隔开) v1.setDirectorNames(directorNames);});三、相同list去重(list里面大部分一致,小部分不一致的,但是这小部分不返给前端)// 原list去重,去掉完全一样的list行,通过taskId随机去重,利用的是treeSet的特性importantTaskListDTOList = importantTaskListDTOList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->new TreeSet(Comparator.comparing(ImportantTaskListDTO::getTaskId))), ArrayList::new));参考:https://blog.csdn.net/Oaklkm/article/details/127074712?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-3-127074712-blog-117334264.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-3-127074712-blog-117334264.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=6四、stream().map().collect(Collectors.toList())用法,把list对象里面的一个列生成对应的一个新list集合List abnormalBuildList = this.list(new QueryWrapper().lambda().eq(AbnormalBuild::getStatus, Status.NORMAL.getCode()).eq(AbnormalBuild::getProjectId, SecurityUtils.getProjectId()));List resourceList = abnormalBuildList.stream().map(AbnormalBuild::getResourceId).collect(Collectors.toList());LIST交集,差集,并集,public class CollectionsTest { public static void main(String[] args) { List list1 = new ArrayList(); list1.add(“1”); list1.add(“2”); list1.add(“3”); list1.add(“4”); list1.add(“5”); List list2 = new ArrayList(); list2.add(“2”); list2.add(“3”); list2.add(“6”); list2.add(“7”); // 交集 List intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList()); System.out.println(“—交集 intersection—“); intersection.parallelStream().forEach(System.out::println); // 差集 (list1 – list2) List 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 reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList()); System.out.println(“—差集 reduce2 (list2 – list1)—“); reduce2.parallelStream().forEach(System.out::println); // 并集 List listAll = list1.parallelStream().collect(toList()); List listAll2 = list2.parallelStream().collect(toList()); listAll.addAll(listAll2); System.out.println(“—并集 listAll—“); listAll.parallelStream().forEachOrdered(System.out::println); // 去重并集 List listAllDistinct = listAll.stream().distinct().collect(toList()); System.out.println(“—得到去重并集 listAllDistinct—“); listAllDistinct.parallelStream().forEachOrdered(System.out::println); }}五、stream().map(v->{})这种方法可以,把list对象里面的多列生成对应的一个新list集合,常用来返回DTO里面包含了List的封装public CompanyDeptTreeDTO selectNewDeptList(String companyId){//根据公司id查询公司BCompany bCompany= remoteTreeService.infoById(companyId,SecurityConstants.INNER).getData();CompanyDeptTreeDTO companyDeptTreeDTO = new CompanyDeptTreeDTO();companyDeptTreeDTO.setResourceId(bCompany.getResourceId());companyDeptTreeDTO.setLevel(bCompany.getLevel());companyDeptTreeDTO.setName(bCompany.getName());companyDeptTreeDTO.setShortName(bCompany.getShortName());companyDeptTreeDTO.setNumber(“”);//查询公司下部门集合List departmentsDTOSList = new ArrayList();departmentsDTOSList = remoteTreeService.getNewChildren(companyId,SecurityConstants.INNER).getData().stream().distinct().map(v->{DepartmentsDTO departmentsDTO = new DepartmentsDTO();//departmentsDTO.setResourceId(v.getResourceId());departmentsDTO.setName(v.getName());departmentsDTO.setCode(“”);departmentsDTO.setShortName(“”);return departmentsDTO;}).collect(Collectors.toList());//组装并返回companyDeptTreeDTO.setDepartmentsDTO(departmentsDTOSList);return companyDeptTreeDTO;}六、stream().map用法,同上,另外一个写法//更新活动文件表,编辑一批图片,新图片就新增到数据库,存在则不管,删除的图片就新增更新人更新时间并改变status为0,其它都updateList activityFileList = this.list(new QueryWrapper().lambda().eq(ActivityFile::getStatus, Status.NORMAL.getCode()).eq(ActivityFile::getActivityId, resourceId));//编辑图片:新增、删除List preUrls = activityFileList.stream().map(v -> v.getFileUrl()).collect(Collectors.toList());七、stream().collect(Collectors.toMap)用法,生成一个map,map键是XX名称,map值是这个名称对应的对象// 查询出listList dbMileposts = milepostNodeService.selectAllByTenantId(TELANTID_OF_FIRST_BUILD_COMPANY, null);// 遍历成mapMap milepostMap = dbMileposts.stream().collect(Collectors.toMap(DbProcessMilepostNode::getMilepostName, obj -> obj));八、Function.identity()用法,一般和toMap对应使用List important = importantTaskService.getImportantListByUserId(userId, taskStatus, keyword);//处理登陆用户在同一个重点任务中既是负责人同时也是被抄送人或被预警人,只显示负责人那条能编辑的重点任务;如果只是被抄送人、被预警人则显示一条不能编辑的重点任务;下面的代码表示按照taskId去分组放到Map中,然后对比Map中的一组的多个TaskListDetailDTO的UserType的值,如果等于0则放回Map中,如果不等于0则返回Map collect = important.stream().collect(Collectors.toMap(TaskListDetailDTO::getTaskId, Function.identity(), (v1, v2) -> Objects.equals(v1.getUserType(), 0) ? v1 : v2));Collection values = collect.values();System.out.println(values);九、stream().sorted用法,对list里面的对象按照时间排序List list = new ArrayList();DateDemo dateDemo = new DateDemo();dateDemo.setId(1);dateDemo.setDate(new Date());list.add(dateDemo);dateDemo = new DateDemo();dateDemo.setId(2);dateDemo.setDate(new Date());list.add(dateDemo);lambda排序:// 时间升序//List listDemo = list.stream().sorted(Comparator.comparing(DateDemo::getDate)).collect(Collectors.toList());// 时间降序List listDemo = list.stream().sorted(Comparator.comparing(DateDemo::getDate).reversed()).collect(Collectors.toList());十、stream().allMatch用法taskStatusList = perTaskDetailInfo.stream().map(TaskDetailInfo::getTaskStatus).collect(Collectors.toList());ifPreTaskComplete = Optional.ofNullable(taskStatusList.stream().allMatch(obj -> !obj.equals(2))).orElse(ifPreTaskComplete);if(taskStatusList.size()!=0){//,如果完成,则更新为启动,如果未完成则继续下面的逻辑 for(int i=0;i<taskStatusList.size();i++){ if(!taskStatusList.get(i).equals(2)){ ifPreTaskComplete = false; break; } }}以上代码都是本人在具体各个项目中用到的,然后总结出来,希望有所帮助