简体   繁体   English

如何与 Comparator.comparing 进行比较?

[英]How to compare with Comparator.comparing?

Hi, i want to compare and get the max date.嗨,我想比较并获得最大日期。 My problem is the next one:我的问题是下一个:

i got this codeline:我得到了这个代码行:

    Optional<User> userOptional = users.stream().max(Comparator.comparing(User::getStudent::getDate));

This is how my entites are:这就是我的实体:

User{

    Long id;
    Student student;
    ...
}

Student{
    Long id;
    Date startDate;
    ...
}

And i want to get the user with the starDate more recent.我想让 starDate 的用户更新。

Thanks.谢谢。

You cannot chain two method references.您不能链接两个方法引用。 Use lambda instead:请改用 lambda:

Optional<User> userOptional = users.stream().max(Comparator.comparing(user -> user.getStudent().getDate()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM