You could have the following:

public Map<Integer, List<String>> getMap(List<String> strings) {
    return strings.stream().collect(
      Collectors.groupingBy(String::length, HashMap::new, Collectors.toCollection(ArrayList::new))
    );
}

The collector groupingBy(classifier, mapFactory, downstream) can be used to specify which type of map is wanted, by passing it a supplier of the wanted map for the mapFactory. Then, the downstream collector, which is used to collect elements grouped to the same key, is toCollection(collectionFactory), which enables to collect into a collection obtained from the given supplier.

This makes sure that the map returned is a HashMap and that the lists, in each value, are ArrayList. Note that if you want to return specific implementations of map and collection, then you most likely want the method to return those specific types as well, so you can use their properties.

If you only want to specify a collection supplier, and keep groupingBy default map, you can just omit the supplier in the code above and use the two arguments overload:

public Map<Integer, List<String>> getMap(List<String> strings) {
    return strings.stream().collect(
      Collectors.groupingBy(String::length, Collectors.toCollection(ArrayList::new))
    );
}

As a side-note, you could have a generic method for that:

public <K, V, C extends Collection<V>, M extends Map<K, C>> M getMap(List<V> list,
        Function<? super V, ? extends K> classifier, Supplier<M> mapSupplier, Supplier<C> collectionSupplier) {
    return list.stream().collect(
        Collectors.groupingBy(classifier, mapSupplier, Collectors.toCollection(collectionSupplier))
    );
}

The advantage with this declaration is that you can now use it to have specific HashMap of ArrayLists as result, or LinkedHashMap of LinkedListss, if the caller wishes it:

HashMap<Integer, ArrayList<String>> m = getMap(Arrays.asList("foo", "bar", "toto"),
        String::length, HashMap::new, ArrayList::new);
LinkedHashMap<Integer, LinkedList<String>> m2 = getMap(Arrays.asList("foo", "bar", "toto"),
        String::length, LinkedHashMap::new, LinkedList::new);

but, at that point, it may be simpler to directly use the groupingBy in the code...

Answer from Tunaki on Stack Overflow
🌐
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
private Map<String, Choice> toMap(List books) { final Map hashMap = new HashMap<>(); for (final Book book : books) { hashMap.put(book.getISBN(), book); } return hashMap; } You can see we have iterated through the List using enhanced for loop of Java 5 and put each element into a HashMap, where ISBN code is the key, and the book object itself is the value.
🌐
Mkyong
mkyong.com › home › java8 › java 8 – convert list to map
Java 8 – Convert List to Map | mkyong.com
May 26, 2017 - package com.mkyong.java8 public ... = websites; } //getters, setters and toString() } Create a list of the Hosting objects, and uses Collectors.toMap to convert it into a Map....
Top answer
1 of 3
63

You could have the following:

public Map<Integer, List<String>> getMap(List<String> strings) {
    return strings.stream().collect(
      Collectors.groupingBy(String::length, HashMap::new, Collectors.toCollection(ArrayList::new))
    );
}

The collector groupingBy(classifier, mapFactory, downstream) can be used to specify which type of map is wanted, by passing it a supplier of the wanted map for the mapFactory. Then, the downstream collector, which is used to collect elements grouped to the same key, is toCollection(collectionFactory), which enables to collect into a collection obtained from the given supplier.

This makes sure that the map returned is a HashMap and that the lists, in each value, are ArrayList. Note that if you want to return specific implementations of map and collection, then you most likely want the method to return those specific types as well, so you can use their properties.

If you only want to specify a collection supplier, and keep groupingBy default map, you can just omit the supplier in the code above and use the two arguments overload:

public Map<Integer, List<String>> getMap(List<String> strings) {
    return strings.stream().collect(
      Collectors.groupingBy(String::length, Collectors.toCollection(ArrayList::new))
    );
}

As a side-note, you could have a generic method for that:

public <K, V, C extends Collection<V>, M extends Map<K, C>> M getMap(List<V> list,
        Function<? super V, ? extends K> classifier, Supplier<M> mapSupplier, Supplier<C> collectionSupplier) {
    return list.stream().collect(
        Collectors.groupingBy(classifier, mapSupplier, Collectors.toCollection(collectionSupplier))
    );
}

The advantage with this declaration is that you can now use it to have specific HashMap of ArrayLists as result, or LinkedHashMap of LinkedListss, if the caller wishes it:

HashMap<Integer, ArrayList<String>> m = getMap(Arrays.asList("foo", "bar", "toto"),
        String::length, HashMap::new, ArrayList::new);
LinkedHashMap<Integer, LinkedList<String>> m2 = getMap(Arrays.asList("foo", "bar", "toto"),
        String::length, LinkedHashMap::new, LinkedList::new);

but, at that point, it may be simpler to directly use the groupingBy in the code...

2 of 3
16

You could use this solution, if you plan to create a map similar to Map<property_1, List<property_2>>:

Map<String, List<String>> ds= requestList.stream().collect(
    Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
    Collectors.mapping(TagRequest::getProperty_2, Collectors.toList()))
);

If you plan to create a map similar to Map<property_1, Set<property_2>>, you may use:

Map<String, List<String>> ds= requestList.stream().collect(
    Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
    Collectors.mapping(TagRequest::getProperty_2, Collectors.toSet()))
);
🌐
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 - Converting a List to a Map is a frequent task in Java, especially when working with collections of objects. With Java 8 and newer, this process has become much simpler thanks to the Stream API.
🌐
Java67
java67.com › 2018 › 01 › how-to-convert-list-into-map-in-java-8.html
How to Convert a List<V> to Map<K,V> in Java 8 - Example Tutorial | Java67
You can also choose the first object or just concatenate the first and second as per your need. That's all about how to convert a list of objects List<V> to a map of keys and values e.g. Map<K, V>. It's super easy in Java 8.
🌐
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 - @Test public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() { Map<Integer, Animal> map = convertListService .convertListBeforeJava8(list); assertThat( map.values(), containsInAnyOrder(list.toArray())); } Starting with Java 8, we can convert a List into a Map using streams and Collectors:
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java 8 tutorial › java 8 list to map
Java 8 List to Map | Examples on How to Convert List to Map in Java 8
February 10, 2023 - The java util map interface is representing the mapping between the value and the key. To convert list in map is a common task in java programming. Java 8 list to map conversion is a common task; we can say that list is a collection of child interfaces. It is nothing but the ordered collection of objects from which we are storing the duplicate values.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
DZone
dzone.com › coding › java › how to convert list to map in java
How to Convert List to Map in Java - DZone
July 23, 2018 - In the following example, we implement ... } With Java 8, you can convert a List to Map in one line using the stream() and Collectors.toMap() utility methods....
🌐
Coderanch
coderanch.com › t › 742647 › java › Convert-list-map
Convert list to map (Features new in Java 8 forum at Coderanch)
May 28, 2021 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... Hi All, I am trying to create a map containing index as key and value as elements from the list.
Find elsewhere
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Java 8 Convert a List to Map Example - Java Code Geeks
February 25, 2019 - The simplest way to do this is iterating over the List and add each element to the Map by extracting the keys and using the real element as an object. This is exactly how developers do it in the ancient Java world but JDK 1.8 has made it even ...
🌐
Coderanch
coderanch.com › t › 698071 › java › Converting-List-Map
Converting from List To Map? (Features new in Java 8 forum at Coderanch)
Yet, I can't see how to do this with Java 8 syntax. Tried to do something similar like this: But that didn't work. Suggestions appreciated. Thanks, -- mike ... For the second argument, use either word -> word or Function.identity(). Note that this collector will throw an exception if it encounters two Words with the same value. If that is a real possibility you need to supply it with a merge function as well.
🌐
Java Guides
javaguides.net › 2018 › 12 › java-8-convert-list-to-map-example.html
Java: Convert List to Map Example
May 30, 2024 - This guide will cover different ways to convert a List to a Map, including using the Collectors.toMap method with various scenarios such as handling duplicate keys and specifying different map implementations.
🌐
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 - However, instead of putting just the last name into the value, we may want to store the entire User object as a value. To do that, we can use Function.identity() function. Let’s group the users into a Map by their Id fields.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › java › how to convert list to map in java
How to convert List to Map in Java | GoLinuxCloud
August 21, 2023 - Here, the result is a map where the grade level is the key, and the value is a list of students in that grade. This way, you can quickly see how many students are in each grade and who they are. Both Map.compute and Map.merge are methods introduced in Java 8 to help in updating the value for a specific key in a map.
🌐
javathinking
javathinking.com › blog › convert-list-to-map-java-8
Convert List to Map in Java 8 — javathinking.com
We use the getId method as the key mapper and the employee object itself as the value mapper. import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; class Person { private int id; private String ...
🌐
How to do in Java
howtodoinjava.com › home › collections framework › convert list to map in java: tomap(), duplicate keys and order
Convert List to Map in Java: toMap(), Duplicate Keys and Order
September 23, 2022 - Converting a list to a map is one Collectors.toMap() call when the keys are unique. When keys repeat, the merge function decides which value to keep or how to combine them, and groupingBy() keeps every element in a list per key.
🌐
JanBask Training
janbasktraining.com › community › java › java 8 list into map
Java 8 List<V> into Map<K> | JanBask Training Community
June 2, 2025 - Java 8 streams make it very concise: Map map = employeeList.stream() .collect(Collectors.toMap(Employee::getId, Function.identity())); ... Employee::getId – This is your key extractor.
🌐
YouTube
youtube.com › watch
Convert list to map in java in 2 ways | List to map conversion with java 8 - YouTube
#java8stream #javalanguage #javadeveloper #javadevelopment #javadevelopers #javajobs #java8 #javaprograming #javaprogram #javaprojects #javaprojectswithsou...
Published: October 3, 2023
🌐
YouTube
youtube.com › watch
Convert List into map using java 8 Streams | key should be String value should be length of String - YouTube
📌Java 8 Streams: Convert List into map using java 8 Streams 📌Please do subscribe my channel: https://www.youtube.com/c/javashastra💛 Join my telegram chann...
Published: June 6, 2023