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

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

问题描述

考虑一个类 User

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

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

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

推荐答案

由于您要求使用lambda,因此可以在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();

或者,如果您有日期的getter:

or, if you have a getter for the date:

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

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

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