How to find Max Date in List<Object>?
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();
How to get object from list with maximum date time object
I'm assuming that begin is a java.util.Date or any other Comparable? Then you could do:
public ContactData getLatestContact(ContactData[] contacts) {
return Stream.of(contacts).sorted((a, b) -> DateTimeToolkit.compare(a.begin, b.begin)).findFirst().get();
}
Find object with max date in a list where date property is a String
Assuming that MyObject::getDate
returns a format that is acceptable for LocalDate.parse
, you are nearly correct. You just need to write a lambda expression:
Comparator.comparing(o -> LocalDate.parse(o.getDate()))
comparing
takes a Function<MyObject, T>
. You are supposed to give it a method that takes a MyObject
and returns something (that extends Comparable
) for it to compare.
Learn more about lambdas here.
Getting object with max date property from list of objects Java 8
and the Contact class should not always use the lastUpdated variable
for comparing instances
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.
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.
Get the highest date in a List of object and get another property of its class
Just order by the date and take the Id property from the first item:
var maxDateId = dtp
.OrderByDescending(x => x.TDateTime)
.FirstOrDefault()?.Id;
FirstOrDefault
can return null
if the sequence is empty, which is why I use the null conditional operator (?.
) to access the Id
property. If the sequence is empty, maxDateId
will be null, so you might have to check for this to make your code safe from a NullReferenceException
.
how to find biggest and smallest date in a list?
Why not use Collections.sort() and then take the first/last entries? You can either use natural ordering, or specify your own Comparator.
Note this will sort the collection in-place. It won't give you a new sorted collection.
Sort max date from Object
Your filter callback function is not performing a comparison to filter the correct element. Also, although applying the "maximum" algorithm on the dates as string would be fine in your case (because of the date format you have), it would be much safer to transform the date strings into date objects to consistantly get correct results regardless of the format.
In the solution below, you can use a combination of Array.map()
and Array.sort()
to copy and process your data in the correct result.
const data = [{
'scores': [{
'score': 10,
'date': '2021-06-05T00:00:00'
}],
'id': '3212'
}, {
'scores': [{
'score': 10,
'date': '2021-06-05T00:00:00'
}, {
'score': 20,
'date': '2021-05-05T00:00:00'
}],
'id': '123'
}, {
'scores': [{
'score': 5,
'date': '2021-05-05T00:00:00'
}],
'id': '321'
}];
// map the data and return the updated objects as the result
const result = data.map((user) => {
// copy the scores array to not mutate the original data
const sortedScores = user.scores.slice();
// sort the scores array by date descending
sortedScores.sort((a, b) => (new Date(b.date) - new Date(a.date)));
// return the same user with the first score from the sorted array
return {
...user,
scores: [sortedScores[0]]
};
});
console.log(result);
How to get max date (Future) from an object array swift
If you're only looking for the date, instead of the LatestReceiptInfo with the highest date, than you could compactMap
the dates (takes out nil-values) and then get the highest one with the max
function.
let highestDate = latestReceiptInfoArray
.compactMap { $0.expiresDate }
.max()
If you want to get the LatestReceiptInfo with the highest date, various approaches will do the trick. For instance by filtering all values that actually have a date. After that you can force-unwrap (yugh) the date in the comparison.
let highestReceiptInfo = latestReceiptInfoArray
.filter { $0.expiresDate != nil }
.max { $0.expiresDate! < $1.expiresDate! }
Related Topics
Get the Nearest Date to Specific from the List of Dates
How to Upload a Document to Sharepoint With Java
How to Save a File Downloaded With Httpclient into a Specific Folder
Select the Letters After - in a String
Changing Scene When a Button Is Pressed Fxml
Spring Boot JPA - Onetomany Relationship Causes Infinite Loop
Jackson Deserialization Issue for Zoneddatetime
Spring Boot Not Recognizing Application.Properties File
How to Split a Huge Zip File into Multiple Volumes
Correct Way to Implement Http Connection Pooling
How to Acknowledge Current Offset in Spring Kafka for Manual Commit
How to Run Selenium Webdriver in the Background
How to Append a Query Parameter to an Existing Url
Java Lombok: Omitting One Field in @Allargsconstructor
Round Off a Double While Maintaining the Trailing Zero
How to Change the Background Color of a Textfield Without Changing the Border in Javafx
Adding a Custom Http Header to a Spring Boot Ws Call (Wstemplate)