You were almost correct with groupingBy, but the second parameter of that is a Collector, not a Function. Thus I used toMap.

 Map<String, Integer> map = Stream.of("bla", "blabla", "blablabla").distinct()
            .collect(Collectors.toMap(Function.identity(), String::length));
Answer from Eugene on Stack Overflow
🌐
OneCompiler
onecompiler.com › java › 3urmp9w25
Convert List<Map<String,String>> to List<String> - Java - OneCompiler
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
🌐
HomieDevs
homiedevs.com › example › java-8-list-of-objects-to-map-string-list-of-values
java 8 list of objects to map<string, list of values code example
Map<String,List<String>> map = userList.stream() .flatMap(user -> { Map<String,String> um = new HashMap<>(); um.put("names",user.getName()); um.put("age",user.getAge()); um.put("org",user.getOrg()); return um.entrySet().stream(); }) // produces a Stream<Map.Entry<String,String>> .collect(C...
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 04 › 10-examples-of-converting-list-to-map.html
10 Examples of Converting a List to Map in Java 8
Now, let's see how we can do the same in Java 8 by using lambda expression and Stream API, here is my first attempt: Map<String, Book> result = books.stream() .collect(Collectors.toMap(book -> book.getISBN, book -> book)); In the above code ...
Top answer
1 of 3
4

You can create a class Company and then subsequent operations become much simpler.

class Company {
    String company;
    String billType;
    String billPeriod;

    public Company(String company, String billType, String billPeriod) {
        this.company = company;
        this.billType = billType;
        this.billPeriod = billPeriod;
    }

    // getters, setters, toString, etc
}

Initialize the list :

List<Company> list = new ArrayList<>();
list.add(new Company("LG", "A", "09-2018"));
list.add(new Company("LG", "A", "09-2018"));
list.add(new Company("LG", "A", "09-2018"));
list.add(new Company("LG", "B", "01-2019"));
list.add(new Company("LG", "B", "01-2019"));
list.add(new Company("Samsung", "A", "10-2018"));
list.add(new Company("Samsung", "A", "10-2018"));
list.add(new Company("Samsung", "B", "11-2018"));

Now for an example, you can group by company name :

Map<String, Long> map = list.stream().collect(
        Collectors.groupingBy(Company::getCompany, 
                              Collectors.mapping((Company c) -> c, Collectors.counting())));

Now it becomes much easier to perform other operations as you desire. Also, here instead of creating 8 maps you end up dealing with just 1 list.

2 of 3
2

It's really difficult to grouping and counting a map because your map data will be changed after you increase your counter value. Therefore, you must save the original data of the map, and save your counter value to the another map. Join 2 maps after your counting process is complete.

Map<Map<String, Object>, Long> counterData = listBeforeGroup.stream().collect(Collectors.groupingBy(m -> m, Collectors.counting()));

List<Map<String, Object>> listAfterGroup = new ArrayList<>();
for (Map<String, Object> m : counterData.keySet()) {
    Map<String, Object> newMap = new HashMap<>(m);
    newMap.put("count", counterData.get(m));
    listAfterGroup.add(newMap);
}

Update Java 8 approach, not easy to debug

List<Map<String, Object>> listAfterGroup = listBeforeGroup.stream().collect(Collectors.groupingBy(m -> m, Collectors.counting())).entrySet().stream().map(e -> {
    Map<String, Object> newMap = e.getKey();
    newMap.put("count", e.getValue());
    return newMap;
}).collect(Collectors.toList());
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to convert list to map in java
How to Convert List to Map in Java | Baeldung
April 4, 2025 - Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
🌐
Mkyong
mkyong.com › home › java8 › java 8 streams map() examples
Java 8 Streams map() examples | mkyong.com
April 3, 2017 - package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class TestJava8 { public static void main(String[] args) { List<Staff> staff = Arrays.asList( new Staff("mkyong", 30, new BigDecimal(10000)), new Staff("jack", 27, new BigDecimal(20000)), new Staff("lawrence", 33, new BigDecimal(30000)) ); //Before Java 8 List<String> result = new ArrayList<>(); for (Staff x : staff) { result.add(x.getName()); } System.out.println(result); //[mkyong, jack, lawrence] //Java 8 List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList()); System.out.println(collect); //[mkyong, jack, lawrence] } } 3.1 This example shows you how to convert a list of staff objects into a list of StaffPublic objects.
🌐
GeeksforGeeks
geeksforgeeks.org › java › conversion-of-java-maps-to-list
Conversion of Java Map to List - GeeksforGeeks
July 11, 2025 - // Converting values into a list import java.util.*; public class Geeks { public static void main(String[] args) { // Create a sample map Map<String, Integer> m = new HashMap<>(); m.put("Geek1", 100); m.put("Geek2", 200); m.put("Geek3", 300); // ...
Top answer
1 of 4
15

I believe something like this should work:

Map<String,List<String>> map =
    userList.stream()
            .flatMap(user -> {
                Map<String,String> um = new HashMap<>();
                um.put("names",user.getName());
                um.put("age",user.getAge());
                um.put("org",user.getOrg());
                return um.entrySet().stream();
            }) // produces a Stream<Map.Entry<String,String>>
            .collect(Collectors.groupingBy(Map.Entry::getKey,
                                           Collectors.mapping(Map.Entry::getValue,
                                                              Collectors.toList())));

It converts each User to a Map<String,String> (containing the 3 required properties indexed by the required keys), and then groups the entries of all the user maps by their keys.

EDIT:

Here's another alternative that creates the Map.Entrys directly instead of creating the small HashMaps, so it should be more efficient:

Map<String,List<String>> map =
    userList.stream()
            .flatMap (user -> Stream.of (new SimpleEntry<>("names",user.getName()),
                                         new SimpleEntry<>("age",user.getAge()),
                                         new SimpleEntry<>("org",user.getOrg())))
            .collect(Collectors.groupingBy(Map.Entry::getKey,
                                           Collectors.mapping(Map.Entry::getValue,
                                                              Collectors.toList())));
2 of 4
6

Eran's showed you how you can accomplish this with streams. As you can hopefully see, it's incredibly ugly.

If your issue with your procedural version is the amount of code duplication, there are other ways besides streams that we can use to solve that problem.

I would refactor the collection to its own method:

private static List<String> getProperty(List<User> users, Function<User, String> getter) {
    return users.stream().map(getter).collect(Collectors.toList());
}

Map<String,List<String>> map = new HashMap<>();
map.put("names", getProperty(userList, User::getName));
map.put("age",   getProperty(userList, User::getAge));
map.put("org",   getProperty(userList, User::getOrg));
🌐
Baeldung
baeldung.com › home › java › java collections › java map › how to store hashmap inside a list
How to Store HashMap<String, ArrayList> Inside a List
April 3, 2025 - For each book category, there is ... the name of a Java-related book to its list of authors: HashMap<String, List<String>> javaBooksAuthorsMap = new HashMap<>();...
🌐
Medium
medium.com › @alxkm › converting-a-list-to-a-map-in-java-multiple-approaches-1cf111e828cc
Java Interview: Converting a List to a Map in Java: Multiple Approaches | by Alex Klimenko | Medium
July 11, 2025 - The most modern and concise way ... } public int getId() { return id; } public String getName() { return name; } } List<User> users = Arrays.asList( new User(1, "Alice"), new User(2, "Bob"), new User(3, "Charlie") ); Convert ...