总结java8 lambda表达式

  Java8 Map转list

List<String> collect = List.entrySet().stream().map(x -> x.getKey() +","+ x.getValue()).collect(Collectors.toList());

Java8 将List中某个属性取出来为单独的一个集合List

List<String> collect = userVos.stream().map(UserVo::getUserCode).collect(Collectors.toList());


//以中英文正则 逗号 横杠拆分字符串
List<String> goodCodeList = Arrays.asList(a.getIncludeSku().trim().split(",|,|-| |\\s+"));

List<String> ids = userList.stream().map(User::getId).collect(Collectors.toList());

List<String> ids = users.stream().map(e -> e.getId()).collect(Collectors.toList());

两个集合根据特定属性去重

ListA 和 ListB 根据 loginName 属性去重 保留去重后的数据

List<VisitDataCollectVo> result = ListA.stream()
        .filter(d1 ->
                ListB.stream()
                        .noneMatch(d2 -> Objects.equals(d1.getLoginName(), d2.getLoginName()))
        ).collect(Collectors.toList());

 List根据属性去重

        ArrayList<T> t= list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(T::get属性))), ArrayList::new));

 根据多字段分组(xb,zcp)

    //java8 多字段分组  根据线别 品项 分组

     Map<String, Map<String, List<TPwUserInputEntity>>> collectList = allUserlist.stream().collect(
                Collectors.groupingBy(
                        TPwUserInputEntity::getXb, Collectors.groupingBy(TPwUserInputEntity::getZcp)
                )
        )

第二种

  Map<String, List<UserOrgPositionDto>> collect1 = positionByKeyWords.stream().collect(
                                       Collectors.groupingBy(UserOrgPositionDto::getBusinessArea));
									   
  
Map<String, String> orgPositionDtoMap positionByKeyWords.stream().collect(Collectors.toMap(UserOrgPositionDto::getOrgPositionRelId, UserOrgPositionDto::getOrgId));

  Map<String, String> categoryMap = list.stream().collect(
        Collectors.toMap(
            QuestionCategoryEntity::getCategoryCode,
            QuestionCategoryEntity::getCategoryName,
            (entity1, entity2) -> entity1
        )
    );

多字段分组
Map<String, List<B03Vo>> collect = b03Vo.stream()
							.collect(Collectors.groupingBy(u -> u.getVTWEG() + "|" + u.getBUKRS() + "|" + u.getVKORG() + "|" + u.getWERKS()));//多字段分组



    List<DealerBankSaveParam> collect = dealerBankInfos.stream().filter(r -> r.getId() != null).collect(Collectors.toList());
            List<String> sumBankInsert = dealerBankInfos.stream().filter(e -> e.getId() == null).map(DealerBankSaveParam::getId).collect(Collectors.toList());

根据单字段分组

 Map<String, List<TPwUserInputEntity>> collect = glgygList.stream().collect(Collectors.groupingBy(a -> a.getZcp()))

过滤List集合中存在null

            List<TPwUserInputEntity> collect = pwUserInputAllDatelist.stream().filter(a -> a.getSalary() == null || a.getGxTeamId() == null).collect(Collectors.toList());

list计算工资总合计

             BigDecimal sumSalary = list.stream().map(x -> x.getSalary() == null ? BigDecimal.ZERO : x.getSalary()).reduce(BigDecimal.ZERO, BigDecimal::add);//ZFL下总工资

                                      

list 分组求和

              Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(a -> a.getPernr(), Collectors.counting()));

List过滤

               List<TPwUserInputEntity> list = wbList.stream().filter(u -> u.getGxTeamId().contains("GY")).collect(Collectors.toList())

根据单个字段统计和

    int ageSum = allUserInputlist.stream().collect(Collectors.summingInt(User::getAttTime));
            System.out.println("总和 :" + ageSum);
            int ageSumTwo = allUserInputlist.stream().mapToInt(a ->Integer.valueOf(a.getAttTime())).sum();

@Accessors(chain = true) 设置多值需要加链式注解 true 每一次set就会返回当前对象
 sumBankInsertList.forEach(a ->a.setDealerId(null).setDealerName(null).setDealerCode(null));

            sumBankInsertList .stream().map(object -> {
                DealerBankSaveParam objectNew = new DealerBankSaveParam();
                BeanUtils.copyProperties(object, objectNew);
                objectNew.setDealerId("111");
                objectNew.setDealerCode("22");
                objectNew.setDealerName("333");
                return objectNew;
            }).collect(Collectors.toList());



 productInfos.stream().forEach(a ->a.setId(null)
                           .setDealerId(param.getDealerId())
                           .setDealerCode(param.getDealerCode())
                           .setDealerName(param.getDealerName()));

 productInfos.stream().map(a -> {
                           a.setId(null)
                           .setDealerId(param.getDealerId())
                           .setDealerCode(param.getDealerCode())
                           .setDealerName(param.getDealerName());
                                return a;
                            }) .collect(Collectors.toList());

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值