List<CrmAuthorizedInfo> crmAuthorizedInfos =
flowPlanInfoMapper.findAllByEncode(stationForm.getOperatorId(), stationIds);
crmAuthorizedInfos.parallelStream()
.collect(Collectors.groupingBy(CrmAuthorizedInfo::getStationId, Collectors.toList()))
.forEach((k, v) -> {
AuthorizedStationInfo authorizedStationInfo = new AuthorizedStationInfo();
authorizedStationInfo.setStationId(k);
List<AuthorizedPileInfo> authorizedPileInfos = new ArrayList<>();
v.parallelStream().collect(Collectors.groupingBy(CrmAuthorizedInfo::getPileId, Collectors.toList())).forEach((k1, v1) -> {
AuthorizedPileInfo authorizedPileInfo = new AuthorizedPileInfo();
authorizedPileInfo.setPileId(k1);
authorizedPileInfo.setGunIds(v1.stream().map(CrmAuthorizedInfo::getGunId).collect(Collectors.toList()));
authorizedPileInfos.add(authorizedPileInfo);
});
authorizedStationInfo.setPileIds(authorizedPileInfos);
authorizedStationInfos.add(authorizedStationInfo);
});
- 通过流处理,groupingBy()分组将stationId相同的属性合并.会生成一个(key,value)的maplist.
- k是设置的相同电站id,v则是相同k的list集合.然后在对相同stationid下的pile进行分组合并.同样生成了(k,v)的maplist结构.剩下的就是设置属性了.结果就生成了这样的数据.