简介
本文主要讲解Stream对日期字段进行排序时的写法,以及当日期字段为null时的排序策略。或者对多个属性进行排序时的案例
Stream对对象中的某个日期属性进行排序
Student对象
import lombok.Data;
import java.util.Date;
@Data
public class Student {
private String name;
private int age;
private Date birthday;
public Student(String name, int age,Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
}
List<Student> list = new ArrayList<>();
Student s1 = new Student("a", 11, new Date(2020, 1, 1));
Student s2 = new Student("b", 12, new Date(2020, 1, 2));
Student s3 = new Student("c", 13, new Date(2020, 1, 3));
list.add(s1);
list.add(s2);
list.add(s3);
list = list.stream()
.sorted(Comparator.comparing(Student::getBirthday))
.collect(Collectors.toList());
注意:当birthday日期属性为空时,再使用Comparator.comparing排序会报空指针异常,此时需要指定策略,即当日期为空时排在最前面或排在最后面。
对日期属性进行排序,并指定日期为空时的策略
List<Student> list = new ArrayList<>();
Student s1 = new Student("a", 11, new Date(2020, 1, 1));
Student s2 = new Student("b", 12, new Date(2020, 1, 2));
Student s3 = new Student("c", 13, null);
list.add(s1);
list.add(s2);
list.add(s3);
list = list.stream()
.sorted(Comparator
.comparing(Student::getBirthday,Comparator
.nullsFirst(Comparator.naturalOrder())))
.collect(Collectors.toList());
System.out.println(list);
排序策略
nullsFirst():为空时排在最前面
此方法返回比较器,其是空型比较,并认为空值小于非空。null首先通过以下逻辑进行操作:
1.null元素被认为小于non-null(即值是null的小于非空的)。
2.当两个元素都为空时,则认为它们相等。
3.当两个元素都不为空时,指定的Comparator确定顺序。
4.如果指定的比较器为null,则返回的比较器将所有非null元素视为相等。
5.如果指定的比较器可序列化,则返回的比较器可序列化。
nullsLast():为空时排在最后面
方法返回比较器,其是空型比较,并认为比非空更大空值。null首先通过以下逻辑进行操作:
1.null元素被认为大于非null。
2.当两个元素都为空时,则认为它们相等。
3.当两个元素都不为空时,指定的Comparator确定顺序。
4.如果指定的比较器为null,则返回的比较器将所有非null元素视为相等。
5.如果指定的比较器可序列化,则返回的比较器可序列化。
Comparator.naturalOrder 和 Comparator.reverseOrder
很多时候我们会面临这样的场景,那就是排序逻辑不变,一会儿根据升序排序,一会根据降序排序,这个时候如果我们的Comparable 中的排序逻辑可以满足上面的排序,就是排序类型(升序还是降序)是不满足的,这个时候我们就可以配合Comparator,来改变原来默认的排序类型(其实就是升序)
nullsFirst与naturalOrder的结合使用
如下示例:当字段为空时排在最前面,剩下的升序排列
List<Student> list = new ArrayList<>();
Student s1 = new Student("a", 11, new Date(2020, 1, 1));
Student s2 = new Student("b", 12, new Date(2020, 1, 2));
Student s3 = new Student("c", 13, null);
list.add(s1);
list.add(s2);
list.add(s3);
list = list.stream()
.sorted(Comparator.comparing(
Student::getBirthday,Comparator
.nullsFirst(Comparator.naturalOrder())))
.collect(Collectors.toList());
Comparator.nullsFirst(Comparator.naturalOrder()))
空值放前面,剩下的升序排序
Comparator.nullsFirst(Comparator.reverseOrder()))
空值放前面,剩下的倒叙排序
Comparator.nullsLast(Comparator.naturalOrder()))
空值放最后,剩下的升序排序
Comparator.nullsLast(Comparator.reverseOrder()))
空值放最后,剩下的倒叙排序
对对象中的多个属性进行排序
先根据生日排序,再根据年龄排序
List<Student> list = new ArrayList<>();
Student s1 = new Student("a", 11, new Date(2020, 1, 1));
Student s2 = new Student("b", 12, new Date(2020, 1, 2));
Student s3 = new Student("c", 13, null);
Student s4 = new Student("d", 13, null);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list = list.stream()
.sorted(Comparator.comparing(
Student::getBirthday,
Comparator.nullsFirst(Comparator.naturalOrder()))
.thenComparing(Student::getAge))
.collect(Collectors.toList());
System.out.println(list);
- ————————————————
版权声明:本文为CSDN博主「五月天的尾巴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_49114503/article/details/123273054