Проверяет умение использовать Collectors.groupingBy.
Используйте Collectors.groupingBy для группировки элементов по ключу:
Map<String, List<Person>> peopleByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity));List<Person> people = List.of(
new Person("Alice", "Moscow"),
new Person("Bob", "Moscow"),
new Person("Charlie", "London")
);
Map<String, List<Person>> byCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity));
// Результат: {"Moscow": [Alice, Bob], "London": [Charlie]}Map<String, Long> countByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity, Collectors.counting()));
// {"Moscow": 2, "London": 1}Map<String, List<String>> namesByCity = people.stream()
.collect(Collectors.groupingBy(
Person::getCity,
Collectors.mapping(Person::getName, Collectors.toList())
));
// {"Moscow": ["Alice", "Bob"], "London": ["Charlie"]}