Java uses stream to sort dates

Introduction

This article mainly explains how Stream sorts the date field, and the sorting strategy when the date field is null. Or the case when sorting multiple properties

Stream sorts a certain date attribute in the object

Student object

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());

Note: When the birthday date attribute is empty, using Comparator.comparing to sort will report a null pointer exception. At this time, you need to specify a strategy, that is, when the date is empty, it will be ranked first or last.

Sort the date attribute and specify the strategy when the date is empty

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

sorting strategy

nullsFirst(): First when null
This method returns a comparator that compares nulls and considers nulls to be less than non-nulls. Null first operates through the following logic:
1. Null elements are considered smaller than non-null (that is, the value is null is smaller than non-null).
2. When both elements are empty, they are considered equal.
3. When both elements are not empty, the specified Comparator determines the order.
4. If the specified comparator is null, the returned comparator treats all non-null elements as equal.
5. If the specified comparator is serializable, the returned comparator is serializable.

nullsLast(): When it is empty,
the method returns the last comparator, which is a comparison of empty types, and considers that the null value is greater than the non-null value. Null is first manipulated by the following logic:
1. Null elements are considered greater than non-null.
2. When both elements are empty, they are considered equal.
3. When both elements are not empty, the specified Comparator determines the order.
4. If the specified comparator is null, the returned comparator treats all non-null elements as equal.
5. If the specified comparator is serializable, the returned comparator is serializable.

Comparator.naturalOrder and Comparator.reverseOrder
often face such a scenario, that is, the sorting logic remains unchanged, sorting in ascending order for a while, and sorting in descending order for a while. At this time, if the sorting logic in our Comparable can satisfy the above sorting, the sorting type (ascending or descending) is not satisfied. At this time, we can cooperate with Comparator to change the original default sorting type (in fact, ascending order)

The combination of nullsFirst and naturalOrder
is as follows: when the field is empty, it is at the top, and the rest are in ascending order
 

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()))
Null values ​​are placed in front, and the rest are sorted in ascending order
) ) Null values ​​are placed at the end , and the rest are sorted in reverse order




Sort multiple properties in an object

Sort first by birthday, then by age

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);
  • ———————————————
    Copyright statement: This article is an original article of CSDN blogger "Mayday's Tail", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting.
    Original link: https://blog.csdn.net/weixin_49114503/article/details/123273054

Guess you like

Origin blog.csdn.net/m0_63364103/article/details/130683239