如何在列表中找到最大日期< Object>? [英] How to find Max Date in List<Object>?

查看:161
本文介绍了如何在列表中找到最大日期< Object>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个类用户

public class User{
  int userId;
  String name;
  Date date;
}

现在我有列表<用户> 大小为20,如何在不使用手动迭代器的情况下在列表中找到最大日期

Now I have a List<User> of size 20, how can I find the max date in the list without using manual iterator?

推荐答案

由于你要求lambdas,你可以在Java 8中使用以下语法:

Since you are asking for lambdas, you can use the following syntax with Java 8:

Date maxDate = list.stream().map(u -> u.date).max(Date::compareTo).get();

或者,如果你有日期的吸气剂:

or, if you have a getter for the date:

Date maxDate = list.stream().map(User::getDate).max(Date::compareTo).get();

这篇关于如何在列表中找到最大日期&lt; Object&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆