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)
Answer from Alex on Stack Overflow
🌐
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 - Java Map‘s put() method is implemented so that the latest added value overwrites the previous one with the same key.
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-convert-list-to-map-in-java
Program to Convert List to Map in Java - GeeksforGeeks
April 9, 2026 - Iterate through the List. Add the key and value of each object to the Map. ... import java.util.*; class Student { private int id; private String name; Student(int id, String name) { this.id = id; this.name = name; } public int getId() { return ...
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.

🌐
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
Hello guys, suppose you have a list of objects, List and you want to convert that to a Map, where a key is obtained from the object and value is the object itself, how do you do it by using Java 8 stream and lambda expression? Prior to Java 8, you can do this by iterating through the List and populating the map by keys and values.
🌐
Marc Nuri
blog.marcnuri.com › home › java streams: convert list into map
Java Streams: Convert List into Map - Marc Nuri
January 27, 2026 - In this post, I'll cover everything you need to know about converting a List to a Map using Collectors.toMap(): ... If you're using a version before Java 16, you can use a traditional class with getters instead. When you're certain that the List contains no duplicate keys, use the basic two-argument version of Collectors.toMap().
🌐
How to do in Java
howtodoinjava.com › home › collections framework › convert list to map in java
Convert List to Map in Java
September 23, 2022 - In case of duplicate values in the List, we can use the Multimap type that automatically stores multiple values in a list that are mapped to a single key.
🌐
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, we use the Stream API to transform our list. The toMap collector maps the student's ID to their name. The result is a map where IDs are the keys and names are the values. The Collectors.groupingBy method is a powerful collector in the Java Stream API designed to group elements of a stream by some classification.
🌐
Baeldung
baeldung.com › home › java › java collections › converting list to map with a custom supplier
Converting List to Map With a Custom Supplier | Baeldung
November 13, 2025 - We start by defining the method with source string and both List and Map suppliers: public Map<Integer, List> collectorToMapByStringLength(List source, Supplier<Map<Integer, List>> mapSupplier, Supplier<List> listSupplier) We then define how ...
🌐
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
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. All you need to know is how to use the collect() and Collector class of Stream API to do this task.
Find elsewhere
🌐
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 import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class TestListMap { public static void main(String[] args) { List<Hosting> list = new ArrayList<>(); list.add(new Hosting(1, "liquidweb.com", 80000)); list.add(new Hosting(2, "linode.com", 90000)); list.add(new Hosting(3, "digitalocean.com", 120000)); list.add(new Hosting(4, "aws.amazon.com", 200000)); list.add(new Hosting(5, "mkyong.com", 1)); // key = id, value - websites Map<Integer, String> result1 = list.stream().collect( Collectors.toMap(Hosting::ge
🌐
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 - By default, Collectors.toMap() produces a HashMap. If you want a different type (e.g., LinkedHashMap to preserve order): Map<Integer, String> userMap = users.stream() .collect(Collectors.toMap( User::getId, User::getName, (v1, v2) -> v1, LinkedHashMap::new )); The Stream API is ideal for converting a List to a Map in a concise and flexible way, especially for modern Java applications.
🌐
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 - With Java 8, you can convert a ... :: getName)); return studentsMap; } The Collectors.toMap() method collects a stream as a Mapand uses its arguments to decide what key/value to use....
🌐
Attacomsian
attacomsian.com › blog › java-convert-list-to-map
How to convert a list to a map in Java
October 29, 2022 - You can also use the product name field as a key, as shown below: Map<String, Double> map = products.stream() .collect(Collectors.toMap(Product::getName, Product::getPrice)); System.out.println(map); // {Honey=9.99, Cookies=3.5, Milk=2.3, Noodles=0.99} If the product list contains duplicate names, an error will be thrown by the Collectors.toMap() method:
🌐
Java67
java67.com › 2017 › 10 › java-8-convert-arraylist-to-hashmap-or.html
How to convert ArrayList to HashMap and LinkedHashMap in Java 8 - Example Tutorial | Java67
For example, suppose you have a List<Order>, and you want to convert it into a Map, e.g. Map<OrderId, Order>, how do you that? Well, the simplest way to achieve this is iterating over List and add each element to the Map by extracting keys ...
🌐
Vultr Docs
docs.vultr.com › java › examples › convert map (hashmap) to list
Java Program to Convert Map (HashMap) to List
December 12, 2024 - Reverse a NumberFind Largest Element ... stringsPass method call as arguments to another method ... In Java, maps and lists are two of the most commonly used data structures in the collections framework. Maps, such as HashMap, store items in key-value pairs, whereas lists store items ...
🌐
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.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › get values and keys as arraylist from a hashmap
Get Values and Keys as ArrayList From a HashMap | Baeldung
March 7, 2025 - One such scenario involves a direct association between the elements keyList[i] and valueList[i] within the original map, given an index i. In essence, elements at corresponding indices in the key and value lists exhibit correlation within the context of the original map: Map: k1 -> v1 k2 -> v2 k3 -> v3 index : 0, 1, 2 KeyList : k1, k2, k3 ValueList: v1, v2, v3 · The second scenario is relatively straightforward. Here, we aim to extract a key list and a value list from the provided map without retaining concern for the initial key-value pair associations.