Java8 lambda表达式收集汇总

2020-11-14  本文已影响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))));```


代码块



代码块



代码块



代码块

上一篇 下一篇

猜你喜欢

热点阅读