wjbk

导航

java8 分组排序

//先根据姓名分组再根据分数排序
Map<String, List<Student>> map1 = listAll.stream().collect(
   Collectors.groupingBy(Student::getName, HashMap::new,
     Collectors.collectingAndThen(Collectors.toList(),
     //正序
     list -> list.stream().sorted(Comparator.comparing(Student::getScore))
     //倒序
     //list -> list.stream().sorted(Comparator.comparing(Student::getScore).reversed())
      .collect(Collectors.toList())
)));

 

posted on 2023-03-06 20:15  wjbk  阅读(295)  评论(0)    收藏  举报