Say I have the following collection of Student
objects which consist of Name(String), Age(int) and City(String).
I am trying to use Java's Stream API to achieve the following sql-like behavior:
SELECT MAX(age)
FROM Students
GROUP BY city
Now, I found two different ways to do so:
final List<Integer> variation1 =
students.stream()
.collect(Collectors.groupingBy(Student::getCity, Collectors.maxBy((s1, s2) -> s1.getAge() - s2.getAge())))
.values()
.stream()
.filter(Optional::isPresent)
.map(Optional::get)
.map(Student::getAge)
.collect(Collectors.toList());
And the other one:
final Collection<Integer> variation2 =
students.stream()
.collect(Collectors.groupingBy(Student::getCity,
Collectors.collectingAndThen(Collectors.maxBy((s1, s2) -> s1.getAge() - s2.getAge()),
optional -> optional.get().getAge())))
.values();
In both ways, one has to .values() ...
and filter the empty groups returned from the collector.
Is there any other way to achieve this required behavior?
These methods remind me of over partition by
sql statements...
Thanks
Edit: All the answers below were really interesting, but unfortunately this is not what I was looking for, since what I try to get is just the values. I don't need the keys, just the values.