转载

List数组使用stream根据时间进行排序实现

效果#

乱序
[
Student{userName='张三', userNick='2', age=22, createTime='2022-12-02 2:11:00'}, 
Student{userName='李四', userNick='1', age=23, createTime='2022-12-03 16:11:00'}, 
Student{userName='王五', userNick='4', age=24, createTime='2022-12-01 21:11:00'}, 
Student{userName='赵六', userNick='3', age=25, createTime='2022-12-02 22:11:00'}]

升序
[
Student{userName='王五', userNick='4', age=24, createTime='2022-12-01 21:11:00'}, 
Student{userName='赵六', userNick='3', age=25, createTime='2022-12-02 22:11:00'},
Student{userName='张三', userNick='2', age=22, createTime='2022-12-02 2:11:00'}, 
Student{userName='李四', userNick='1', age=23, createTime='2022-12-03 16:11:00'}]

降序[
Student{userName='李四', userNick='1', age=23, createTime='2022-12-03 16:11:00'}, 
Student{userName='张三', userNick='2', age=22, createTime='2022-12-02 2:11:00'}, 
Student{userName='赵六', userNick='3', age=25, createTime='2022-12-02 22:11:00'}, 
Student{userName='王五', userNick='4', age=24, createTime='2022-12-01 21:11:00'}]

代码实现#

public class Test {
    public static void main(String[] args) throws IOException {
        //测试数据,请不要纠结数据的严谨性
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("张三","2",22,"2022-12-02 2:11:00"));
        studentList.add(new Student("李四","1",23,"2022-12-03 16:11:00"));
        studentList.add(new Student("王五","4",24,"2022-12-01 21:11:00"));
        studentList.add(new Student("赵六","3",25,"2022-12-02 22:11:00"));
        //根据日期进行升序排序
        //乱序
        System.out.println("乱序"+studentList);
        //根据日期进行升序排序
        List<Student> studentsSortAsce = studentList.stream().sorted(Comparator.comparing(Student::getCreateTime)).collect(Collectors.toList());
        //根据日期进行降序排序
        List<Student> studentsSortDesc = studentList.stream().sorted(Comparator.comparing(Student::getCreateTime).reversed()).collect(Collectors.toList());
        //升序后输出
        System.out.println("升序"+studentsSortAsce);
        //降序后输出
        System.out.println("降序"+studentsSortDesc);
        

    }


}

class Student{
    private String userName;
    private String userNick;
    private int age;
    private String createTime;

    public Student() {
    }

    public Student(String userName, String userNick, int age, String createTime) {
        this.userName = userName;
        this.userNick = userNick;
        this.age = age;
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "Student{" +
                "userName='" + userName + '\'' +
                ", userNick='" + userNick + '\'' +
                ", age=" + age +
                ", createTime='" + createTime + '\'' +
                '}';
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserNick() {
        return userNick;
    }

    public void setUserNick(String userNick) {
        this.userNick = userNick;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
}

reversed() 进行一个反转的操作 Student::此处填写要排序的字段
正文到此结束
Loading...