简体   繁体   English

从对象列表中获取具有最大日期属性的对象 Java 8

[英]Getting object with max date property from list of objects Java 8

I have a class called Contact that has a Date lastUpdated;我有一个名为Contact的类,它有一个Date lastUpdated; variable.多变的。

I would like to pull the Contact out of a List<Contact> that has the max lastUpdated variable.我想从具有最大lastUpdated变量的List<Contact>中拉出Contact

I know that this can be done by writing a custom comparator and using Collections.max , but I was wondering if there is a way this can be done in Java 8 that does not require using a custom comparator, since I just want to pull the one with a max date in just one spot in my code, and the Contact class should not always use the lastUpdated variable for comparing instances.我知道这可以通过编写自定义比较器并使用Collections.max来完成,但我想知道是否有一种方法可以在不需要使用自定义比较器的 Java 8 中完成,因为我只想拉一个在我的代码中只有一个位置具有最大日期,并且Contact类不应该总是使用lastUpdated变量来比较实例。

Writing custom comparator in Java-8 is very simple.在 Java-8 中编写自定义比较器非常简单。 Use:用:

Comparator.comparing(c -> c.lastUpdated);

So if you have a List<Contact> contacts , you can use因此,如果您有List<Contact> contacts ,则可以使用

Contact lastContact = Collections.max(contacts, Comparator.comparing(c -> c.lastUpdated));

Or, using method references:或者,使用方法引用:

Contact lastContact = Collections.max(contacts, Comparator.comparing(Contact::getLastUpdated));

and the Contact class should not always use the lastUpdated variable for comparing instances并且 Contact 类不应该总是使用 lastUpdated 变量来比较实例

So you will have to provide a custom comparator whenever you want to compare multiple instances by their lastUpdated property, as it implies that this class is not comparable by default with this field.因此,每当您想通过lastUpdated属性比较多个实例时,您都必须提供自定义比较器,因为这意味着默认情况下该类无法与此字段进行比较。

Comparator<Contact> cmp = Comparator.comparing(Contact::getLastUpdated);

As you know you can either use Collections.max or the Stream API to get the max instance according to this field, but you can't avoid writing a custom comparator.如您所知,您可以使用Collections.max或 Stream API 根据此字段获取最大实例,但您无法避免编写自定义比较器。

尝试以下(未经测试):

contacts.stream().max(Comparator.comparing(Contact::getLastUpdated)).get()

在定义合适的Comparator后使用List<T>.stream().max(Comparator<T>).get()

Since you wanted to do it using java 8 lambdas, use this (assuming you have List<Contact> contacts ):由于您想使用 java 8 lambdas 来完成它,请使用它(假设您有List<Contact> contacts ):

Contact latest = contacts.stream()
     .sorted(Comparator.<Contact, Date>comparing(contact-> contact.getLastUpdated(), Comparator.reverseOrder()))
     .findFirst().get();

You can customize this comparator quite easily by simply appending further comparator.您可以通过简单地附加更多比较器来轻松自定义此比较器。 So the code is easily modifiable.所以代码很容易修改。

List<Contact> sortedContacts = contacts.stream()
         .sorted(Comparator.<Contact, Date>comparing(contact-> contact.getLastUpdated(), Comparator.reverseOrder())
               .thenComparing(contact -> contact.getAge()))
         .collect(Collectors.toList());

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

相关问题 从对象列表 java 中获取具有最大字符大小的属性 - Get Property that has max Character size from list of objects java Java从对象列表中获取每个对象中的属性列表时防止代码重复 - Java Prevent code duplication when getting a list of a property in each Object from a list of Objects 在列表中查找具有最大日期的对象,其中date属性为String - Find object with max date in a list where date property is a String 如何使用 Java 中的 object 属性对列表中的对象进行分组 - How to group objects in a list with object property in Java Java 8:从列表列表中获取属性 - Java 8: Getting a property from a List of a List 使用Java 8获取属性的最大值 - Getting the max of a property using Java 8 Java - 按属性组合和最近日期过滤对象列表 - Java - filter list of objects by property combo and most recent date 如何从List对象中的新对象中设置属性值,并获得使用Java 8设置属性的相同对象的新List? - How to set property values in new object from object from List and get a new List of the same objects in which properties are set using java 8? 如何按日期对对象列表进行排序(Java 集合、列表<Object> ) - How to sort a List of objects by their date (java collections, List<Object>) 从Java中的列表中获取特定类型的对象 - Getting specific type of objects from list in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM