java使用lambda表达式多条件排序

一、使用java的lambda表达式多条件排序,这里多条件是指同时生效
先把我的对象摆上

@Data
@AllArgsConstructor
@ToString
public class Student { 
    private String name;
    private String age;
    private Integer id;
    private Integer score;
}

然后再准备好排序的数据

List<Student> studentList = new ArrayList<>();
studentList.add(new Student("1", "222", 5, 8));
studentList.add(new Student("2", "111", 3, 9));
studentList.add(new Student("3", "224", 4, 6));
studentList.add(new Student("4", "333", 3, 11));
studentList.add(new Student("4", "444", 5, 6));
System.out.println(studentList);

这里的构造按照实体里的属性先后定的位置,1条件代表id,2条件代表score
有以下集中场景:

System.out.println("例一--------------1升2升------------");
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).thenComparing(Student::getScore)).collect(Collectors.toList());
for (Student s : studentList) { 
    System.out.println(s);
}
System.out.println("例二--------------1升2降------------");
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed().thenComparing(Student::getScore).reversed()).collect(Collectors.toList());
for (Student s : studentList) { 
    System.out.println(s);
}
System.out.println("例三--------------2降1升------------");
studentList = studentList.stream().sorted(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getId)).collect(Collectors.toList());
for (Student s : studentList) { 
    System.out.println(s);
}
System.out.println("例四--------------1降2升------------");
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed().thenComparing(Student::getScore)).collect(Collectors.toList());
for (Student s : studentList) { 
    System.out.println(s);
}
System.out.println("例五--------------1降2降------------");
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).thenComparing(Student::getScore).reversed()).collect(Collectors.toList());
for (Student s : studentList) { 
    System.out.println(s);
}

结果:

--------------12------------
Student(name=2, age=111, id=3, score=9)
Student(name=4, age=333, id=3, score=11)
Student(name=3, age=224, id=4, score=6)
Student(name=4, age=444, id=5, score=6)
Student(name=1, age=222, id=5, score=8)
--------------12------------
Student(name=4, age=333, id=3, score=11)
Student(name=2, age=111, id=3, score=9)
Student(name=3, age=224, id=4, score=6)
Student(name=1, age=222, id=5, score=8)
Student(name=4, age=444, id=5, score=6)
--------------21------------
Student(name=4, age=333, id=3, score=11)
Student(name=2, age=111, id=3, score=9)
Student(name=1, age=222, id=5, score=8)
Student(name=3, age=224, id=4, score=6)
Student(name=4, age=444, id=5, score=6)
--------------12------------
Student(name=4, age=444, id=5, score=6)
Student(name=1, age=222, id=5, score=8)
Student(name=3, age=224, id=4, score=6)
Student(name=2, age=111, id=3, score=9)
Student(name=4, age=333, id=3, score=11)
--------------12------------
Student(name=1, age=222, id=5, score=8)
Student(name=4, age=444, id=5, score=6)
Student(name=3, age=224, id=4, score=6)
Student(name=4, age=333, id=3, score=11)
Student(name=2, age=111, id=3, score=9)

       在这里注意,1升2降和2升1降有区别的,谁在前谁优先级高;第一、三、四个例子都好理解,第二、五需要思考一下,这里的reversed()妙用。reversed()是把列表倒过来,所以1升2降,就是先把1降,然后再把1、2都倒置过来,这样就是1升2降了,所以1降2降就是再后面都倒置过来。使用lambda表达式排序还是很nice啊,节省不少操作,但数据量最好不要太大。

    原文作者:完美明天cxp
    原文地址: https://blog.csdn.net/weixin_43155301/article/details/106649571
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞