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

这里多条件是指同时生效

先把我的对象摆上

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

然后再准备好排序的数据

List 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); 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);
}

结果:

拥有名称为2, 年龄是111, 身份标识为3, 分数为9。

姓名是4, 年龄为333, 身份号码是3, 分数是11。

名字是3, 年龄为224,, 身份编号是4, 分数是6。

名为4, 年龄是444, 身份标识为5, 得分是6。

名为1, 年龄是222, 标号为5, 分数为8。

一会是升, 一会的降, 一会一下升, 一会一下降, 一会升完该降了, 一会降完又升了。

(name=4, age=333, id=3, score=11)

(name=2, age=111, id=3, score=9)

(name=3, age=224, id=4, score=6)

(name=1, age=222, id=5, score=8)

(name=4, age=444, id=5, score=6)

- 从2的位置往下降, 然后有1个位置是上升的状态。

(name=4, age=333, id=3, score=11)

(name=2, age=111, id=3, score=9)

(name=1, age=222, id=5, score=8)

(name=3, age=224, id=4, score=6)

(name=4, age=444, id=5, score=6)

(name=4, age=444, id=5, score=6)

(name=1, age=222, id=5, score=8)

java lambda表达式排序优先级_java lambda 排序_java lambda表达式多条件排序

(name=3, age=224, id=4, score=6)

(name=2, age=111, id=3, score=9)

(name=4, age=333, id=3, score=11)

是要将1降低, 然后再把2降低, 这样的表述,这样可以吗。

(name=1, age=222, id=5, score=8)

(name=4, age=444, id=5, score=6)

(name=3, age=224, id=4, score=6)

(name=4, age=333, id=3, score=11)

(name=2, age=111, id=3, score=9)

在这里注意

1升2降跟2升1降是存在区别的, 谁处在前面谁的优先级就高, 第一个例子好理解, 第三个例子好理解, 第四个例子也好理解, 第二个例子需要思考一番, 第五个例子需要思考一番, 这里面()有着妙用。

这是将列表进行颠倒操作, 因此呈现出1升2降的情况, 具体是先把1降低, 接着再把1、2整体进行倒置, 如此便形成了1升2降的结果, 所以1降2降是在后续整体再次进行倒置。

采用表达式进行排序, 真的是相当不错, 能够节省好多操作, 不过数据量最好别太大。

总结

这是属于个人的经验, 期望能够给予大家一份可供参考的内容, 并且也期望大家能够给予脚本之家诸多的支持。

更多推荐