Collectors.toMap write a merge function on a property of object which is not used a value
To select a person based on its age, you need the Person
instance to query the age. You cannot reconstitute the information after you mapped the Person
to a plain name String
.
So you have to collect the persons first, to be able to select the oldest, followed by mapping them to their names:
persons.stream()
.collect(Collectors.groupingBy(Person::getNationality, Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingInt(Person::getAge)),
o -> o.get().getName())));
If you don't want to use a helper data structure, it is possible if you first keep your Person info and perform the merge based on it and apply the mapping afterwards:
public void test() {
final List<Person> persons = new ArrayList<>();
final BinaryOperator<Person> mergeFunction =
(lhs, rhs) -> lhs.getAge() > rhs.getAge() ? lhs : rhs;
final Function<Person, String> mapFunction = Person::getName;
final Map<String, String> personNamesByNation =
persons.stream()
.collect(
Collectors.groupingBy(Person::getNation, // KeyMapper Person.getNation: Map<String, List<Person>>
Collectors.collectingAndThen(
Collectors.collectingAndThen(
Collectors.reducing(mergeFunction), // Merge Persons into single value via merge function: Map<String, Optional<Person>>
Optional::get), // unwrap value: Map<String, Person>
mapFunction))); // apply map function afterwards: Map<String, String>
}