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啊,节省不少操作,但数据量最好不要太大。

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值