yourTypes.stream()
         .collect(Collectors.groupingBy(
               ObjectA::getType,
               Collectors.mapping(
                    ObjectA::getId, Collectors.toList()
)))
Answer from Eugene 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
In the past, we have seen how to use the Collectors.groupingBy() method to group elements in Set and In this article, we will use Collectors.toMap() method to convert a List of an object into a Map in Java.
๐ŸŒ
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....
๐ŸŒ
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
Map<String, Course > result = listOfCourses .stream() .collect( Collectors.toMap(Course::getTitle, Function.identity(), (e1, e2) -> e2, LinkedHashMap::new)); Here in case of a duplicate, we are using the second element as a key to generated LinkedHashMap. 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.
๐ŸŒ
Java4s
java4s.com โ€บ how to convert list to map in java 8
How to Convert List to Map in Java 8
October 26, 2016 - import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ListtoMap { public static void main(String[] args) { List<Account> li = new ArrayList<Account>(); Account ac1= new Account(100,"Savings","Open"); Account ac2= new Account(101,"Checking","Closed"); li.add(ac1); li.add(ac2); // Before Java 8 Map<Integer, String> beforeJava8 = new HashMap<>(); for (Account acc: li) { beforeJava8.put(acc.getAccNo(), acc.getAccStatus()); } System.out.println("Before Java 8 "+beforeJava8); // Java 8 Using Lambda expression and Stream API Map<Integer, String> map = li.stream().collect(Collectors.toMap(x -> x.getAccNo() , x -> x.getAccStatus())); System.out.println("Java 8 "+map); } }
๐ŸŒ
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.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 698071 โ€บ java โ€บ Converting-List-Map
Converting from List To Map? (Features new in Java 8 forum at Coderanch)
August 21, 2018 - You already have the key mapper, you need a value mapper. So the second argument would be: word -> word, or somewhat more stylish: Function.identity(). In that last case: why does the method reference: Function::identity not work? There are three kinds of actuaries: those who can count, and those who can't.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2018 โ€บ 12 โ€บ java-8-convert-list-to-map-example.html
Java: Convert List to Map Example
May 30, 2024 - ... Converting a List to a Map in Java 8 can be accomplished using the Collectors.toMap method in various ways. You can handle duplicate keys and specify different map implementations to suit your needs.
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.
๐ŸŒ
Makeinjava
makeinjava.com โ€บ home โ€บ convert list of objects to /from map in java 8 lambda stream (example)
Convert list of objects to /from map in java 8 lambda stream (with example)
January 3, 2024 - Given the list of person objects or POJO, convert list of objects to map and convert map to list of objects using java 8 lambda streams
๐ŸŒ
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 ...
Top answer
1 of 16
491

With java-8, you'll be able to do this in one line using streams, and the Collectors class.

Map<String, Item> map = 
    list.stream().collect(Collectors.toMap(Item::getKey, item -> item));

Short demo:

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Test{
    public static void main (String [] args){
        List<Item> list = IntStream.rangeClosed(1, 4)
                                   .mapToObj(Item::new)
                                   .collect(Collectors.toList()); //[Item [i=1], Item [i=2], Item [i=3], Item [i=4]]

        Map<String, Item> map = 
            list.stream().collect(Collectors.toMap(Item::getKey, item -> item));

        map.forEach((k, v) -> System.out.println(k + " => " + v));
    }
}
class Item {

    private final int i;

    public Item(int i){
        this.i = i;
    }

    public String getKey(){
        return "Key-"+i;
    }

    @Override
    public String toString() {
        return "Item [i=" + i + "]";
    }
}

Output:

Key-1 => Item [i=1]
Key-2 => Item [i=2]
Key-3 => Item [i=3]
Key-4 => Item [i=4]

As noted in comments, you can use Function.identity() instead of item -> item, although I find i -> i rather explicit.

And to be complete note that you can use a binary operator if your function is not bijective. For example let's consider this List and the mapping function that for an int value, compute the result of it modulo 3:

List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6);
Map<String, Integer> map = 
    intList.stream().collect(toMap(i -> String.valueOf(i % 3), i -> i));

When running this code, you'll get an error saying java.lang.IllegalStateException: Duplicate key 1. This is because 1 % 3 is the same as 4 % 3 and hence have the same key value given the key mapping function. In this case you can provide a merge operator.

Here's one that sum the values; (i1, i2) -> i1 + i2; that can be replaced with the method reference Integer::sum.

Map<String, Integer> map = 
    intList.stream().collect(toMap(i -> String.valueOf(i % 3), 
                                   i -> i, 
                                   Integer::sum));

which now outputs:

0 => 9 (i.e 3 + 6)
1 => 5 (i.e 1 + 4)
2 => 7 (i.e 2 + 5)
2 of 16
223
List<Item> list;
Map<Key,Item> map = new HashMap<Key,Item>(list.size());
for (Item i : list) map.put(i.getKey(),i);

Assuming of course that each Item has a getKey() method that returns a key of the proper type.

๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ convert-a-list-of-objects-to-map-java-8-inline
Convert a List of Objects to Map in Java 8 Inline โ€” javathinking.com
When converting a list of objects to a map, we can use the stream() method to convert the list into a stream, and then use the collect() method to collect the elements of the stream into a map.
๐ŸŒ
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 - To preserve the list items order in a map, we need to pass another parameter to the method of Collectors.toMap, which was deciding what type of map we are using. The below example shows how we can convert the list to map by using the object of list method as follows.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 04 โ€บ 10-examples-of-converting-list-to-map.html
Javarevisited: 10 Examples of Converting a List to Map in Java 8
In the past, we have seen how to use the Collectors.groupingBy() method to group elements in Set and In this article, we will use Collectors.toMap() method to convert a List of an object into a Map in Java.
๐ŸŒ
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....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ program-to-convert-list-to-map-in-java
Program to Convert List to Map in Java - GeeksforGeeks
September 4, 2026 - Create a List of objects. Convert the List into a Stream using stream(). Use Collectors.toMap() to specify the key and value. Collect the elements into a Map. ... import java.util.*; import java.util.stream.Collectors; class Student { private ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 742647 โ€บ java โ€บ Convert-list-map
Convert list to map (Features new in Java 8 forum at Coderanch)
May 28, 2021 - Win a copy of OCP Java Programmer SE 25 Practice Exams 1Z0-831 this week in the Programmer Certification (OCPJP) forum! ... 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.