MYBLOG

欢迎来到和风柒夜的博客~

java lambda表达式应用

2024-03-26 14:33:47Java

  • List<对象>根据某些字段去重
List<QuoteLastAssessmentDetailDTO> lastAssessmentDetails = lastAssessmentDetailAll.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> 
new TreeSet<>(Comparator.comparing(p -> p.getCustomerId()+";"+p.getOuterMaterialCode()))), ArrayList::new));
  • List<对象>转Map<属性,List<属性>>
  Map<Long, List<String>> materialCodeMap = recordDetails.stream()
.collect(Collectors.groupingBy(QuoteRecordDetail::getQuoteCustomId,
 Collectors.mapping(QuoteRecordDetail::getOuterMaterialCode, Collectors.toList())));

List<对象>转Map<属性,List<另一个对象>

Map<String, List<PersonDTO>> cityToPersonDTOs = persons.stream()
                .collect(Collectors.groupingBy(
                        Person::getCity,
                        Collectors.mapping(person -> new PersonDTO(person.getName(), person.getAge()), Collectors.toList())
                ));