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

 更新时间:2023年09月04日 15:09:34   作者:完美明天cxp  
这篇文章主要介绍了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啊,节省不少操作,但数据量最好不要太大。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java二叉树的非递归遍历

    java二叉树的非递归遍历

    二叉树的递归遍历比较简单,这里就不聊了,今天主要聊聊二叉树的非递归遍历,主要借助于“栈”后进先出的特性来保存节点的顺序,先序遍历和中序遍历相对来说比较简单,重点理解后序遍历
    2020-12-12
  • SpringMVC 重新定向redirect请求中携带数据方式

    SpringMVC 重新定向redirect请求中携带数据方式

    这篇文章主要介绍了SpringMVC 重新定向redirect请求中携带数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 使用@value注解取不到application.xml配置文件中的值问题

    使用@value注解取不到application.xml配置文件中的值问题

    这篇文章主要介绍了使用@value注解取不到application.xml配置文件中的值问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Springmvc自定义参数转换实现代码解析

    Springmvc自定义参数转换实现代码解析

    这篇文章主要介绍了Springmvc自定义参数转换实现代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • 2020macOS Big Sur配置Java开发环境之jdk安装过程

    2020macOS Big Sur配置Java开发环境之jdk安装过程

    这篇文章主要介绍了2020macOS Big Sur配置Java开发环境之jdk安装,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-02-02
  • 使用SpringBoot与Thrift实现RPC通信的方式详解

    使用SpringBoot与Thrift实现RPC通信的方式详解

    在微服务架构的世界里,服务间的通信机制选择成为了关键决策之一,RPC因其简洁、高效的特点备受青睐,本文将详细探讨如何利用Spring Boot和Thrift框架构建RPC通信,让读者理解其内在原理及实现方式,需要的朋友可以参考下
    2023-10-10
  • 详解Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送

    详解Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送

    这篇文章主要介绍了Jenkins 实现Gitlab事件自动触发Jenkins构建及钉钉消息推送,应该会对大家学习Jenkins有所启发
    2021-04-04
  • SpringBoot的WebSocket实现单聊群聊

    SpringBoot的WebSocket实现单聊群聊

    这篇文章主要为大家详细介绍了SpringBoot的WebSocket实现单聊群聊,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-02-02
  • Java基础之方法重写详解

    Java基础之方法重写详解

    这篇文章主要介绍了Java基础之方法重写详解,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-05-05
  • 关于springboot集成阿里云短信的问题

    关于springboot集成阿里云短信的问题

    这篇文章主要介绍了springboot集成阿里云短信的方法,本文通过实例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11

最新评论