对List集合中每个对象元素按时间顺序排序

首先创建一个实体类 

package com.huawei.Test;

import java.util.Date;

/**
 * @author h84250472
 * @title: User$
 * @description: TODO
 * @date 2022/6/29 10:54
 */
public class User {
    private String name;
    private String sex;
    private Integer age;
    private Date birthday;

    public User(String name, String sex, Integer age, Date birthday) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.birthday = birthday;
    }

    public User() {
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

 之后通过把实体类加入到list集合中,并按照生日时间进行排序,此处用了两种方法进行排序,分别是stream和Collections进行排序,代码如下:

package com.huawei.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author h84250472
 * @title: Test04$
 * @description: TODO
 * @date 2022/7/25 14:44
 */
public class Test04 {
    public static void main(String[] args) throws ParseException {
        List list = new ArrayList<>();
        long time1 = System.currentTimeMillis()+10000L;
        long time2 = System.currentTimeMillis()+20000L;
        long time3 = System.currentTimeMillis()+30000L;
        long time4 = System.currentTimeMillis()+40000L;
        //规定日期格式
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将long类型的时间变成String类型
        String format1 = sd.format(time1);
        //将String类型的时间变成Date类型
        Date parse1 = sd.parse(format1);
        String format2 = sd.format(time2);
        Date parse2 = sd.parse(format2);
        String format3 = sd.format(time3);
        Date parse3 = sd.parse(format3);
        String format4 = sd.format(time4);
        Date parse4 = sd.parse(format4);
        User u1 = new User("zahngSan","nan",20,parse1);
        User u2 = new User("lisi","nan",30,parse2);
        User u3 = new User("wangwu","nan",26,parse3);
        User u4 = new User("xiaofang","nv",20,parse4);
        Collections.addAll(list,u1,u2,u3,u4);
//        List collect = list.stream().sorted(new Comparator() {
//            @Override
//            public int compare(User o1, User o2) {
//                Date date1 = o1.getBirthday();
//                Date date2 = o2.getBirthday();
//                return Long.compare(date2.getTime(), date1.getTime());
//            }
//        }).collect(Collectors.toList());

        //用stream进行排序
        List collect = list.stream().sorted((User o1, User o2) -> {
                Date date1 = o1.getBirthday();
                Date date2 = o2.getBirthday();
                return Long.compare(date2.getTime(), date1.getTime());
            }
        ).collect(Collectors.toList());
        for (User user : collect) {
            System.out.println(user);
        }
        //用集合进行排序
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(User o1, User o2) {
                Date date1 = o1.getBirthday();
                Date date2 = o2.getBirthday();
                if (date1.getTime()>date2.getTime()){
                    return 1;
                } else if (date1.getTime()

你可能感兴趣的:(Java基础,list,java,jvm)