JDK8 Stream的分组功能Collectors.groupingBy两种写法

  • Post author:
  • Post category:其他


话不多说直接上代码

//按自定义格式对数据集合List进行分组
//List.stream().collect(Collectors.groupingBy(对象Vo -> {分组自定义标志字段}))
List<TestRecord> TestRecordsByMonthList = mapper.selectTestRecordByCondition(plantId, startDate, endDate);
Map<String, List<TestRecord>> mapGroupByMonth = TestRecordsByMonthList .stream().collect(Collectors.groupingBy(testRecord -> {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    return sdf.format(testRecord.getPlanSamplingTime());
}));

//常规方式,按数据集合List内元素特定字段进行分组
//List.stream().collect(Collectors.groupingBy(对象Vo::分组标志字段))
List<Equipment> equipmentList = operateCenterMapper.selectEquipmentFailureGroupByOrgId(plantId,startDate,endDate);
Map<String, List<Equipment>> mapGroupByOrg = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getOrgId));



版权声明:本文为m0_37692562原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。