首页 > 其他分享 >List数组使用stream根据时间进行排序实现

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

时间:2022-10-27 17:12:36浏览次数:67  
标签:userName 00 stream userNick age List Student 排序 createTime

乱序
[
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;
    }
}

 

标签:userName,00,stream,userNick,age,List,Student,排序,createTime
From: https://www.cnblogs.com/mihutao/p/16832917.html

相关文章

  • *PAT_甲级_1053 Path of Equal Weight (30分) (C++)【数组排序/DFS】
    目录​​1,题目描述​​​​ 题目大意​​​​输入​​​​输出​​​​2,思路​​​​数据结构 ​​​​如何排序 ​​​​如何设计DFS算法​​​​3,心路历程​​​​4,代......
  • LeetCode_LinkedList_138. Copy List with Random Pointer 复制带随机指针的链表(C++)【
    目录​​一,题目描述​​​​英文描述​​​​中文描述​​​​二,解题思路​​​​三,AC代码​​​​C++​​​​四,解题过程​​​​第一博​​一,题目描述英文描述Alinkedli......
  • LeetCode_LinkedList_19. Remove Nth Node From End of List 删除链表的倒数第 N 个结
    目录​​一,题目描述​​​​英文描述​​​​中文描述​​​​二,解题思路​​​​三,AC代码​​​​C++​​​​四,解题过程​​​​第一博​​ 一,题目描述英文描述Giventhe......
  • LeetCode_LinkedList_82. Remove Duplicates from Sorted List II 删除排序链表中的重
    目录​​一,题目描述​​​​英文描述​​​​中文描述​​​​二,解题思路​​​​三,AC代码​​​​C++​​​​四,解题过程​​​​第一博​​一,题目描述英文描述Giventheh......
  • 数据结构与算法分析——第七章 排序
    注:发此文谨以记录初学《数据结构与算法分析——C语言描述》的个人理解,希望能够得到宝贵意见与建议。(文中转载有相关文章片段,在学习时帮助理解作用较大,在此对作者表示感谢)7.1......
  • Java8新特性3:Stream流
    回顾之前《JavaSE-23.2》:https://www.cnblogs.com/yppah/p/14900824.htmlhttps://www.cnblogs.com/yppah/p/14900829.htmlhttps://www.cnblogs.com/yppah/p/14900834.ht......
  • Java中getResourceAsStream方法返回null
    原因:我存放资源的文件并不是源根,所以我们的方法是无法访问到的。所以返回null指针!解决办法:鼠标右键点击要读取配置文件的文件夹将文件夹标记成ResourcesRoot就可以......
  • JPA 中 in 的方法 注意参数一定要是List
    JPA中in的方法注意参数一定要是List可以是List<String>或者List<int>@Query(nativeQuery=true,value="SELECTaea.`create_date`AScreateTime,aadd.`rem......
  • C# 实现排序算法
    C#实现各种排序每种排序的要点和实现目录C#实现各种排序冒泡排序选择排序插入排序希尔排序堆排序归并排序快速排序文章中参数Func<T,T,bool>comp的意思是:排序后对于......
  • AList
    AListUsage#https://alist.nn.ci/zh/guide/install/docker.html#PORT:5244dockerpullxhofe/alistdockerrun--detach=true\--name=alist\--publish......