Bootstrap

小记:java stream的使用--分组

java stream的使用–分组

/finished是list,根据getSafetyPotentialRiskClass(),getProcessFlag()分类
//filter用来过滤为空的情况
Map<String, Map<String,Long>> map = finished
        .stream()
        .filter(e-> StringUtils.isNotBlank(e.getSafetyPotentialRiskClass())&&StringUtils.isNotEmpty(e.getSafetyPotentialRiskClass()))
        .collect(Collectors.groupingBy(ConstructionSafety::getSafetyPotentialRiskClass,Collectors.groupingBy(ConstructionSafety::getProcessFlag,Collectors.counting())));
//合并相同uid的数据,将他们的getAssignComplete()相加
//k是uid,v是uid相等的列表
list.parallelStream().collect(Collectors.groupingBy(ScheduleQuantityWeek::getUid,Collectors.toList()))
        .forEach((k,v)->{
            ScheduleQuantityWeek temp=v.get(0);
            for(ScheduleQuantityWeek item:v){
                temp.setAssignComplete(temp.getAssignComplete()+item.getAssignComplete());
                temp.setFinish(item.getFinish());
            }
            res.add(temp);
        });
;