stream流根据时间筛选list最新数据

24 篇文章 0 订阅
4 篇文章 0 订阅
public static void main(String[] args) throws ParseException {
        List<Student> list = new ArrayList<>();
        list.add(new Student("zhangsan",10,TimeUtils.stringToDate("2022-07-07 22:20:41")));
        list.add(new Student("zhangsan",30,TimeUtils.stringToDate("2022-07-07 22:20:43")));
        list.add(new Student("zhangsan",20,TimeUtils.stringToDate("2022-07-07 22:20:42")));
        list.add(new Student("zhangsan",40,TimeUtils.stringToDate("2022-07-07 22:20:46")));
        list.add(new Student("lisi",30,TimeUtils.stringToDate("2022-07-07 22:20:43")));

        Map<String, List<Student>> map = list.stream().collect(Collectors.groupingBy(Student::getName));
        map.keySet().forEach(key -> {
            List<Student> students = map.get(key);
            //int maxAge = l.stream().map(s -> s.getAge()).mapToInt(Integer::intValue).max().getAsInt();
            List<Date> dateList = students.stream().map(Student::getBirth).collect(Collectors.toList());
            Date date = dateList.stream().max(Date::compareTo).get();
            List<Student> maxAgeStu = students.stream().filter(s -> s.getBirth().getTime() == date.getTime()).collect(Collectors.toList());
            System.out.println(maxAgeStu);
        });
    }
    /*public static void main(String[] args) throws ParseException {
        List<Student> list = new ArrayList<>();
        list.add(new Student("zhangsan",10,TimeUtils.stringToDate("2022-07-07 22:20:41")));
        list.add(new Student("zhangsan",30,TimeUtils.stringToDate("2022-07-07 22:20:43")));
        list.add(new Student("zhangsan",20,TimeUtils.stringToDate("2022-07-07 22:20:42")));
        list.add(new Student("lisi",30,TimeUtils.stringToDate("2022-07-07 22:20:43")));
        Map<String, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getName));
        collect.keySet().forEach(key->{
            List<Student> students = collect.get(key);
            List<Student> collect1 = students.stream().sorted(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    long time = o1.getBirth().getTime();
                    long time1 = o2.getBirth().getTime();
                    if (time>time1) {
                        return 1;
                    }
                    return -1;
                }
            }).collect(Collectors.toList());
            Date birthday = collect1.get(collect1.size()-1).getBirth();
            List<Student> maxAgeStu = students.stream().filter(
                    s -> s.getBirth().getTime()==birthday.getTime()).collect(Collectors.toList());
            System.out.println(maxAgeStu);
        });
    }*/

实体类

/**
 * @author およそ神
 * @version JDK 1.8
 */
@Data
public class Student {
    String name;
    Integer age;
    Date birth;

    public Student(String name, Integer age,Date birth) {
        this.name = name;
        this.age = age;
        this.birth = birth;
    }
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值