美文网首页
Java8 lambda表达式收集汇总

Java8 lambda表达式收集汇总

作者: 码而优则仕 | 来源:发表于2020-11-14 11:19 被阅读0次

    1 有一个对象列表,根据对象中某个字段对列表进行分组,分组后获取组中对象列表中某个字段最大的值:

    customerList.stream().filter(customer -> null != customer.getCreateDate())
                    .collect(Collectors.groupingBy(cn.com.servyou.xqy.gaia.facade.customer.dto.DzbCustomerDTO::getCustomerId,
                            Collectors.collectingAndThen(Collectors.mapping(cn.com.servyou.xqy.gaia.facade.customer.dto.DzbCustomerDTO::getCreateDate, Collectors.toSet()), set ->
                                    set.stream().max(Date::compareTo).orElse(null))));
    
    

    2 有一个对象列表,根据对象中某个字段对对象列表进行分组,分组后对每组对象中的某个字段求和

     Map<String, Integer> customerIdPrintNumMap = printResult.stream().collect(Collectors.groupingBy(CustomerFinanceInfoPrintRecordDTO::getCustomerId, Collectors
                    .collectingAndThen(Collectors.mapping(CustomerFinanceInfoPrintRecordDTO::getPrintNum, Collectors.toList()), list ->
                            list.stream().mapToInt(Integer::intValue).sum()
    

    有一个对象列表,根据对象中某个字段对对象列表进行分组,对组内对象列表根据对象某个字段在组内排序后取最小的对象

     Map<String, DzbWebCustomerBossRelationshipDTO> dzbCustomerMap = dzbWebCustomerBossRelationshipFacadeClient.queryByCustomerIdList(companyId, customerIdList).stream().filter(a -> StringUtils.isNotBlank(a.getCustomerId()))
                    .collect(Collectors.groupingBy(DzbWebCustomerBossRelationshipDTO::getCustomerId, Collectors.collectingAndThen(Collectors.toList(),
                            list -> list.stream().min(Comparator.comparing(DzbWebCustomerBossRelationshipDTO::getCreateDate)).orElse(null))));```
    
    
    

    代码块

    
    
    

    代码块

    
    
    

    代码块

    
    
    

    代码块

    相关文章

      网友评论

          本文标题:Java8 lambda表达式收集汇总

          本文链接:https://www.haomeiwen.com/subject/xgwibktx.html