一.对象针对某个属性去重
SysEntertainSchemeStandardDTOList = lstDTO.stream().filter(e -> e.getEntertainLevel()!=null && e.getEntertainLevel()!="").collect(
Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing( u-> u.getEntertainLevel()))), ArrayList::new));
二.对某个值去重并且排除为空的
operatorUserIds = memberEntityList.stream().map(FscFgMemberEntity::getOperatorUserId).filter(StrUtil::isNotBlank).distinct().collect(Collectors.toList())
三.使用stream对list集合排序
方法一 : 使用 Stream 进行排序时,如果排序的字段出现 null 值就会导致异常发生
list = list.stream().sorted(Comparator.comparing(Person::getAge).reversed())
.collect(Collectors.toList())
方法二:Comparator.nullsFirst 表示将排序字段中的 null 值放到集合最前面,如果想要将 null 值放到集合最后面可以使用 Comparator.nullsLast
list = list.stream().sorted(Comparator.comparing(Person::getAge,
Comparator.nullsFirst(Integer::compareTo)))
.collect(Collectors.toList());
四.使用stream进行排序
按照List中对象的id属性升序
list.sort(Comparator.comparing(Stu::getId))
按照List中对象的id属性降序
list.sort(Comparator.comparing(Stu::getId).reversed());
多条件升序
list.sort(Comparator.comparing(Stu::getId).thenComparing(Stu::getSid));
多条件降序
list.sort(Comparator.comparing(Stu::getId).reversed().thenComparing(Stu::getSid))
分组
Map<String, List<OpEfsPaymentDTO>> collect = dtoList.stream().collect(Collectors.groupingBy(
OpEfsPaymentDTO::getBoeHeaderId));
stream遍历重新赋值
private List<Application> buildApplications(List<ApplicationDTO> dtoList, Long collectionId) {
return dtoList.stream().map(p -> buildApplicationByDto(p, collectionId)).collect(Collectors.toList());
}
private Application buildApplicationByDto(ApplicationDTO dto, Long collectionId) {
Application application = new Application();
application.setCollectionId(collectionId);
application.setAppType(dto.getAppType());
application.setPaper(dto.getPaper());
application.setCopies(dto.getCopies());
application.setPlateNo(dto.getPlateNo());
application.setVehId(dto.getVehId());;
return application;
}