Imagine you have a list of people, Roberts, Pauls, Richards, etc, these are people grouped by name into Map<String, List<Person>>
. You want to find the oldest Paul, Robert, etc... You can do it like so:
public static void main(String... args) {
List<Person> people = Arrays.asList(
new Person(23, "Paul"),
new Person(24, "Robert"),
new Person(32, "Paul"),
new Person(10, "Robert"),
new Person(4, "Richard"),
new Person(60, "Richard"),
new Person(9, "Robert"),
new Person(26, "Robert")
);
Person dummy = new Person(0, "");
var mapping = people.stream().collect(groupingBy(Person::getName, reducing(dummy, (p1, p2) -> p1.getAge() < p2.getAge() ? p2 : p1)));
mapping.entrySet().forEach(System.out::println);
}
Say, I want to get a mapping in the form of Map<String, Integer>
instead of Map<String, Person>
, I can do it like so:
var mapping = people.stream().collect(groupingBy(Person::getName, mapping(Person::getAge, reducing(0, (p1, p2) -> p1 < p2 ? p2 : p1))));
The steps above are:
- Group by name into
Map<String/*Name*/, List<Person>>
- Map that
List<Person>
intoList<Integer>
- Find maximum Integer in those lists.
I was wondering how to do:
- Group by name into
Map<String, List<Person>>
- Find the oldest person in each group name, getting
Map<String, Person>
- Convert
Map<String, Person>
intoMap<String, Integer>
. And I want to do all that inside that chain of groupingBy's, reducing's and mapping's.
This is the "pseudocode":
var mapping = people.stream().collect(groupingBy(Person::getName, reducing(dummy, (p1, p2) -> p1.getAge() < p2.getAge() ? p2 : p1 /*, have to write some other collector factory method here*/)));
How can I achieve this?
Map<String, Integer>
? – shmosel May 30 '18 at 0:06[java-stream]
tag. There is no requirement to acknowledge the fact that this API was introduced in Java 8 by tagging every[java-stream]
question with[java-8]
. The version specific tags are there to state that you want a version specific answer. Since APIs may change, most notably, they get extended and there might be simpler solutions using the new API version, that’s a restriction. But restricting the API to one version but actively using newer features in the example code is confusing. – Holger May 30 '18 at 12:31