对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<User> 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<User> collect = list.stream().sorted(new Comparator<User>() {
//            @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<User> 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<User>() {
            @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()<date2.getTime()){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
    }
}

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值