max
requires a Comparator
as parameter.
I think the shortest thing you can do is to call the method compareTo
using method reference.
public Date maxAgeJava8(Collection<Person> persons) {
return persons.stream().map(Person::getBirthday).max(Date::compareTo).get();
}
Also note that you don't need to use a Stream
to accomplish this. You could simply use Collections.max
providing a custom comparator:
public Date maxAgeJava8(Collection<Person> persons) {
return Collections.max(persons, Comparator.comparing(Person::getBirthday)).getBirthday();
}