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
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.

๐ŸŒ
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 - Evidently, we can convert a List ... methods: public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) { Map<Integer, Animal> map = new HashMap<>(); for (Animal animal : list) { map.put(animal.getId(), animal); } return map; } Now we test ...
๐ŸŒ
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
Map<String, Integer> map = new HashMap<>(); for(String str: listOfString){ map.put(str, str.length()); } In this code, I have chosen a HashMap, but you are free to select any kind of map, e.g. LinkedHashMap or TreeMap depending upon your requirement.
๐ŸŒ
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
Here is how you can convert a List to Map in Java 5, 6 or 7: 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 ...
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ convert-arraylist-to-hashmap-in-java
Convert ArrayList to HashMap in Java - GeeksforGeeks
July 23, 2025 - Basically, there are two different ways to convert ArrayList to Hashmap- ... Here, we just need to iterate on each of the elements of the ArrayList and the element can be converted into the key-value pair and store in the HashMap.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ convert-arraylist-to-hashmap-in-java
Convert ArrayList to HashMap in Java
June 17, 2024 - In this possible approach, we are going to apply the ASCII characters table method approach to perform the conversion of an array list into a set of Hash Map. //Java program for list convert in HashMap with the help of ASCII characters table methods import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class ARBRDD{ public static void main(String[] args){ List<String> chars = Arrays.asList("A", "R", "B", "U"); Map<String, Integer> asciiMap = new HashMap<>(); for (String s : chars){ if (asciiMap.put(s, s.hashCode()) != null){ throw new IllegalStateException("Duplicate key"); } } System.out.println(asciiMap); } }
๐ŸŒ
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 - Map<Integer, List> employeeMap... Java version 1.8, we can have Streams and collectors to convert a List into a Map by using toMap() method....
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ program-to-convert-list-to-map-in-java
Program to Convert List to Map in Java - GeeksforGeeks
July 11, 2025 - Using Collectors.toMap() method: This method includes creation of a list of the student objects, and uses Collectors.toMap() to convert it into a Map. Approach: ... // Java program for list convert in map // with the help of Collectors.toMap() ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java collections โ€บ java map โ€บ how to store hashmap inside a list
How to Store HashMap Inside a List | Baeldung
April 3, 2025 - In this article, we talked about storing HashMaps inside a List in Java. Then, we wrote a simple example in which we added HashMap<String, ArrayList<String>> to a List<String> for Two book categories.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-add-an-ArrayList-to-a-Java-HashMap
How to add an ArrayList to a Java HashMap - Quora
... What you need is Map which has as key the Keyword from your csv data and as value a Listwhich holds all the Alternate values corresponding to a Keyword. Map<String, List<String>> alternateMap = new HashMap<>();
๐ŸŒ
ZetCode
zetcode.com โ€บ java โ€บ list2hashmap
Java List to HashMap Conversion
These methods are essential for transforming data structures in Java applications, such as mapping indices to elements or grouping objects by properties. A straightforward approach to convert a List to a HashMap is using a for-loop, mapping list indices to elements.
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ how to convert list to map in java
How to Convert List to Map in Java
July 23, 2018 - public static Map<Integer,String> ... } With Java 8, you can convert a List to Map in one line using the stream() and Collectors.toMap() utility methods....
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_hashmap.asp
Java HashMap
It is part of the java.util package and implements the Map interface. Instead of accessing elements by an index (like with ArrayList), you use a key to retrieve its associated value. A HashMap can store many different combinations, such as:
๐ŸŒ
Quora
quora.com โ€บ How-do-you-add-an-element-to-an-arraylist-thats-in-a-hashmap
How to add an element to an arraylist that's in a hashmap - Quora
Answer (1 of 6): [code] HashMap carsList = cars.get(mapKey); // if list does not exist create it if(carsList == null) { carsList = ...
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java8 โ€บ java 8 โ€“ convert list to map
Java 8 - Convert List to Map - Mkyong.com
May 26, 2017 - Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites)); System.out.println("Result 1 : " + result1); } } Output โ€“ The error message below is a bit misleading, it should show โ€œlinodeโ€ instead of the value of the key. Exception in thread "main" java.lang.IllegalStateException: Duplicate key 90000 at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133) at java.util.HashMap.merge(HashMap.java:1245) //... 2.2 To solve the duplicated key issue above, pass in the third mergeFunction argument like this : Map<String, Long> result1 = list.stream().collect( Collectors.toMap(Hosting::getName, Hosting::getWebsites, (oldValue, newValue) -> oldValue ) ); Output ยท