Use the simple groupingBy variant:

Map<String, List<Pojo>> map = pojos
  .stream()
  .collect(
     Collectors.groupingBy(Pojo::getKey)
  );
Answer from Eran on Stack Overflow
🌐
Medium
medium.com › geekculture › java-streams-collect-to-map-of-lists-with-grouping-by-8cb309df4598
Java Streams: Collect to Map of Lists with Grouping By | Geek Culture
May 7, 2022 - How to collect a stream into a map of lists, to avoid duplicate key exception, using the Collectors . groupingBy method method reference lambda functions
🌐
Mkyong
mkyong.com › home › java8 › java 8 – stream collectors groupingby examples
Java 8 – Stream Collectors groupingBy examples | mkyong.com
August 10, 2016 - package com.mkyong.java8; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; public class Java8Example2 { public static void main(String[] args) { //3 apple, 2 banana, others 1 List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya"); Map<String, Long> result = items.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); Map<String, Long> finalMap = new LinkedHashMap<>(); //Sort a map and add to
🌐
Baeldung
baeldung.com › home › java › java streams › guide to java groupingby collector
Guide to Java groupingBy Collector | Baeldung
November 10, 2025 - The first approach is to use Collectors::collectingAndThen for the downstream collector of groupingBy. For the first parameter of collectingAndThen we collect the stream into a list, using Collectors::toList.
🌐
Medium
medium.com › @mtl98 › grouping-lists-to-maps-in-java-5922d9c8b71e
Grouping Lists to Maps in Java. Welcome everyone , in Java… | by Mohammed Taoufik Lahmidi | Medium
December 13, 2024 - A common scenario is to group objects based on a property they possess. For instance, let’s say we have a list of Student objects, and we want to group them by country . Map<Integer, List<Student>> studentByCountry = students.stream() .collect(Collectors.groupingBy(Student::getCountry));
🌐
How to do in Java
howtodoinjava.com › home › java streams › java 8 stream – collectors groupingby with examples
Java 8 Stream - Collectors GroupingBy with Examples
August 16, 2023 - This will create a Map<Department, List<Person>> where map key is department records and map value will be all the persons in that department. Map<Department, List<Person>> map = persons.stream().collect(groupingBy(Person::department)); ...
🌐
Educative
educative.io › answers › how-to-group-elements-into-a-map-using-collectors-in-java
How to group elements into a map using Collectors in Java
Map<Integer, List<Student>> groupedStudents = studentList.stream().collect(Collectors.groupingBy(s -> s.grade));
🌐
amitph
amitph.com › home › java › examples of converting list to map using streams
Examples of Converting List to Map using Streams | amitph
November 22, 2024 - Let’s group the users into a Map by their Id fields. List<User> ——-> Map<Long, User> Map<Long, User> map = users.stream() .collect (Collectors.toMap(User::getId, Function.identity()));Code language: Java (java) Now, we get a Map of the ...
Find elsewhere
🌐
4Comprehension
4comprehension.com › home › the ultimate guide to the java stream api groupingby() collector
The Ultimate Guide to the Java Stream API groupingBy() Collector
February 21, 2026 - If you need to provide a custom Map implementation, you can do that by using a provided groupingBy() overload: List<String> strings = List.of("a", "bb", "cc", "ddd"); TreeMap<Integer, List<String>> result = strings.stream() .collect(groupin...
🌐
Medium
salithachathuranga94.medium.com › java-8-streams-groupby-b15054d9e6c8
Java 8 Streams — GroupBy. Let’s understand how to group objects… | by Salitha Chathuranga | Medium
August 7, 2022 - We need to group the employee names by their age. How to do this using streams? Map<Integer, List<String>> employeeNamesByAge = employees.stream() .collect(Collectors.groupingBy( Employee::getAge, Collectors.mapping(Employee::getName, Collectors.toList()) ) ); Employee::getAge — Employee age getter as method reference [Function] Collectors.mapping(Employee::getName, Collectors.toList()) — collects the elements using getter for name [Collector] => this will map employee names as a list.
🌐
DZone
dzone.com › coding › javascript › the ultimate guide to the java stream api groupingby() collector
The Ultimate Guide to the Java Stream API groupingBy() Collector
September 19, 2018 - If you need to provide a custom Map implementation, you can do that by using a provided groupingBy() overload: List<String> strings = List.of("a", "bb", "cc", "ddd"); TreeMap<Integer, List<String>> result = strings.stream() .collect(groupin...
🌐
Stack Abuse
stackabuse.com › guide-to-java-8-collectors-groupingby
Guide to Java 8 Collectors: groupingBy()
March 28, 2023 - Map<String, List<String>> namesByCity = students.stream() .collect(Collectors.groupingBy( Student::getCity, TreeMap::new, Collectors.mapping(Student::getName, Collectors.toList()))); Note: We could've used any other Map implementation that Java offers - like a HashMap or a LinkedHashMap as well. To recap, this code will give us a grouped list of students by the city they're from, and since we're using a TreeMap here, the cities' names will be sorted.
🌐
CodinGame
codingame.com › playgrounds › 3312 › java-8-streams-cookbook › groupby-join-and-grouping
Coding Games and Programming Challenges to Code Better
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
Top answer
1 of 2
145

You need to chain a Collectors.mapping() collector to the Collectors.groupingBy() collector:

Map<String, List<Fizz>> collect =
    docs.stream()
        .collect(Collectors.groupingBy(DistrictDocument::getCity,
                 Collectors.mapping(d->createFizz(d),Collectors.toList())));

If createFizz(d) would return a List<Fizz, you can flatten it using Java 9's Collectors.flatMapping:

Map<String, List<Fizz>> collect =
    docs.stream()
        .collect(Collectors.groupingBy(DistrictDocument::getCity,
                 Collectors.flatMapping(d->createFizz(d).stream(),Collectors.toList())));

If you can't use Java 9, perhaps using Collectors.toMap will help:

Map<String, List<Fizz>> collect =
    docs.stream()
        .collect(Collectors.toMap(DistrictDocument::getCity,
                                  d->createFizz(d),
                                  (l1,l2)->{l1.addAll(l2);return l1;}));
2 of 2
12

In case you'd like to do a doubly nested grouping:

Let's say you have a collection of EducationData objects that contain School name, teacher name, and student name. But you want a nested map that looks like :

What you have

class EducationData {
     String school;
     String teacher;
     String student;

     // getters setters ...
}

What you want

Map<String, Map<String, List<String>>> desiredMapOfMpas ...

// which would look like this :

"East High School" : {
     "Ms. Jackson" : ["Derek Shepherd", "Meredith Grey", ...],
     "Mr. Teresa" : ["Eleanor Shellstrop", "Jason Mendoza", ...],
     ....
}

How to Get There

import static java.util.stream.Collectors.*;

public doubleNestedGroup(List<EducationData> educations) {
     Map<String, Map<String, List<String>>> nestedMap = educations.stream()
        .collect(
             groupingBy(
                  EducationData::getSchool,
                  groupingBy(
                       EducationData::getTeacher,
                       mapping(
                            EducationData::getStudent,
                            toList()
                       )
                  )
              )
        );
}
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Collectors.toMap() vs Collectors.groupingBy() in Java Streams - Java Code Geeks
June 27, 2024 - In summary, Collectors.toMap() is suitable for direct mapping of stream elements to a map, while Collectors.groupingBy() is ideal for grouping elements based on common attributes or criteria, often resulting in a map where each key maps to a ...
🌐
Blogger
javarevisited.blogspot.com › 2015 › 07 › how-to-do-group-by-in-java-8.html
How to do GROUP BY in Java 8? Collectors.groupingBy() Example
Well, we can do that by using a ... way like that, you have a much cleaner solution. You can use Stream and Collector which provides groupingBy() method to do this....
🌐
javaspring
javaspring.net › blog › java-streams-group-a-list-into-a-map-of-maps
Java Streams: How to Group a List into a Map of Maps (Step-by-Step Nested Grouping Guide) — javaspring.net
Nested grouping with Java Streams is a powerful technique for multi-level data categorization. By combining Collectors.groupingBy with downstream collectors, you can create flexible nested maps tailored to your needs—whether grouping lists of elements or aggregating values like sums or counts.