list.stream()

1、list分组:

Map<String,List<AlarmStatisticalDO>> map = alarmStatisticalDOList.stream().collect(Collectors.groupingBy(a -> a.getAlarmDate()));
for (String alarmDateStr:map.keySet()) {
    List<LibAlarm> libAlarmList = new ArrayList<>();
    List<AlarmStatisticalDO> alarmStatisticalDOS = map.get(alarmDateStr);
   ....
}

Map<String,Set<String>> personMap = paramsDto.getDoPersonList().stream().collect(
        Collectors.groupingBy(CompleteParamsDto.DoPerson :: getPersonId,
                Collectors.mapping(CompleteParamsDto.DoPerson::getItemName,Collectors.toSet()))
);

 

2、list排序:

List<LibAlarm> libAlarms = libAlarmList.stream().sorted(Comparator.comparing(LibAlarm::getAlarmCount).reversed()).collect(Collectors.toList());//倒序

 

3、过滤

Set<String> doBackUser = paramsDto.getDisposalList().stream().filter(a -> a.getDisposalStatus().equals(CommonConstants.THREE))
                    .map(a -> a.getUserId()).collect(Collectors.toSet());

 4、返回另一个集合

List<DataCountDto> dataCountDtoList = communityList.stream().map(community -> {
                DataCountDto dataCountDto = new DataCountDto();
                dataCountDto.setCommunityCode(community.getCommunityCode());
                dataCountDto.setTotal(community.getCount());
                return dataCountDto;
            }).collect(Collectors.toList());

 

posted on 2021-07-20 12:03  LJD泊水  阅读(237)  评论(0编辑  收藏  举报