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);
}结果:
--------------1升2升------------
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)
--------------1升2降------------
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)
--------------2降1升------------
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)
--------------1降2升------------
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)
--------------1降2降------------
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啊,节省不少操作,但数据量最好不要太大。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringMVC 重新定向redirect请求中携带数据方式
这篇文章主要介绍了SpringMVC 重新定向redirect请求中携带数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-12-12
使用@value注解取不到application.xml配置文件中的值问题
这篇文章主要介绍了使用@value注解取不到application.xml配置文件中的值问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-03-03
2020macOS Big Sur配置Java开发环境之jdk安装过程
这篇文章主要介绍了2020macOS Big Sur配置Java开发环境之jdk安装,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-02-02
使用SpringBoot与Thrift实现RPC通信的方式详解
在微服务架构的世界里,服务间的通信机制选择成为了关键决策之一,RPC因其简洁、高效的特点备受青睐,本文将详细探讨如何利用Spring Boot和Thrift框架构建RPC通信,让读者理解其内在原理及实现方式,需要的朋友可以参考下2023-10-10
详解Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送
这篇文章主要介绍了Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送,应该会对大家学习Jenkins有所启发2021-04-04


最新评论