I have a class PlayerAndCountry
:
class PlayerAndCountry {
private Country country;
private Player player;
}
And a List<PlayerAndCountry>
I would like my final Map like this : Map<Country,List<Player>>
I know how to get Map<Country, List<PlayerAndCountry>>
but not Map<Country,List<Player>>
You need to use Collectors.mapping
to achieve it:
Map<Country, List<Player>> collect = list.stream()
.collect(Collectors.groupingBy(PlayerAndCountry::getCountry,
Collectors.mapping(PlayerAndCountry::getPlayer, Collectors.toList()))
);
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen