//按指定字段去重(java8)
List<hysysLeftDataDTO> countLeftData = hysysLeftDataDTOList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(hysysLeftDataDTO::getFirstLeftTitle))),ArrayList::new
));
//按指定字段分组(单字段java8)
Map<String, List<hysysLeftDataDTO>> groupBy = hysysLeftDataDTOList.stream().collect(Collectors.groupingBy(hysysLeftDataDTO::getFirstLeftTitle));
//按指定字段分组(多字段java8)
Map<String, List<LeftDataDTO>> groupBy = leftDataDTOList.stream().collect(
Collectors.groupingBy(
leftDataDTO -> leftDataDTO.getFileCode() +'/'+ leftDataDTO.getFileName() +'/'+ leftDataDTO.getDocumentName()
//单字段排序
firstGroupByCodeNameDoc.sort((a,b)->a.getArea().compareTo(b.getArea()));
//多字段排序(非Java8)
Collections.sort(firstGroupByCodeNameDoc, new Comparator<LeftDataDTO>() {
@Override
public int compare(LeftDataDTO o1, LeftDataDTO o2) {
if(o2.getArea().compareTo(o1.getArea()) != 0){
return o1.getArea().compareTo(o2.getArea());
}
else{
return o1.getDevice().compareTo(o2.getDevice());
}
}
});
));
//循环遍历分组后的值与取值
for (Map.Entry<String, List<hysysLeftDataDTO>> stringListEntry : groupBy.entrySet()) {
//获取分组的value值
List<hysysLeftDataDTO> groupByFirstLeftTitle = stringListEntry.getValue();
CellRange firstTitle = sheet.getCellRange(LeftCount,1);
firstTitle.setValue(stringListEntry.getKey());
java java8 List去重,按指定字段分组,按字段排序操作(持续更新)
最新推荐文章于 2025-03-28 17:27:21 发布