List分组GroupBy一个字段,如下

  Map<Long, List<BaseOil>> subListMap = baseOilList.stream().filter(x -> x.getSubclassId() != null).collect(Collectors.groupingBy(BaseOil::getSubclassId));

 多个字段,可以将多个字段new到一个实体中,得到分组结果,如下

 Map<MtrlSupplyDailyDtlEntity, List<MtrlSupplyDailyDtlEntity>> collect = dtlList.stream().collect(Collectors.groupingBy(
                record -> new MtrlSupplyDailyDtlEntity(record.getBaseOilId(), record.getBaseOilName(), record.getMtrlStoragePlaceId())));

  

对分组结果也可以筛选过滤

Map<ReportEntity, List<InReport>> entry = subListMap.entrySet().stream().filter(
x -> x.getKey().getReportDateT().equals(dateList.get(0))
&& x.getKey().getCompanyCode().equals(code)).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

 

posted on 2021-12-28 14:42  莫等闲也  阅读(3283)  评论(0编辑  收藏  举报