lamba表达式使用技巧
2022-09-27 本文已影响0人
冲鸭_程序猿
### 在一个集合中, 根据对象的某个属性进行去重
List<InquiryCoatedProductDTO> resultList = list.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(inquiryCoatedProductDTO -> inquiryCoatedProductDTO.getMID()))), ArrayList::new)
);
### 根据多个属性同时去重
List<Holiday> result = set.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId() + ";" + o.getHolidayType()))), ArrayList::new)
);