Program to Convert List to Map in Java

Last Updated : 4 Sep, 2026

A List is an ordered collection that can contain duplicate elements, while a Map stores data in the form of key-value pairs. Java provides several ways to convert a List of objects into a Map based on a key and a value.

Example:

Input: List = [Student(1, "Aman"), Student(2, "Riya"), Student(3, "Rahul")]
Output: Map = {1=Aman, 2=Riya, 3=Rahul}

Ways to Convert List to Map in Java

1. Using a Loop

The simplest approach is to create an empty Map and add each object from the List using its key and value.

Approach:

  1. Create a List of objects.
  2. Create an empty Map.
  3. Iterate through the List.
  4. Add the key and value of each object to the Map.
Java
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 id; }

    public String getName() { return name; }
}

class ListToMapLoop {
    public static void main(String[] args)
    {

        List<Student> students = new ArrayList<>();

        students.add(new Student(1, "Aman"));
        students.add(new Student(2, "Riya"));
        students.add(new Student(3, "Rahul"));

        Map<Integer, String> map = new HashMap<>();

        for (Student student : students) {
            map.put(student.getId(), student.getName());
        }

        System.out.println("List converted to Map: " + map);
    }
}

Output
List converted to Map: {1=Aman, 2=Riya, 3=Rahul}

Explanation: Each Student object provides an id as the key and a name as the value. The put() method adds these key-value pairs to the Map.

2. Using Collectors.toMap()

The Collectors.toMap() method can convert a Stream of List elements into a Map. Method references can be used to specify which object fields should become keys and values.

Approach:

  1. Create a List of objects.
  2. Convert the List into a Stream using stream().
  3. Use Collectors.toMap() to specify the key and value.
  4. Collect the elements into a Map.
Java
import java.util.*;
import java.util.stream.Collectors;

class Student {
    private int id;
    private String name;

    Student(int id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public int getId() { return id; }

    public String getName() { return name; }
}

class ListToMapStream {
    public static void main(String[] args)
    {

        List<Student> students = new ArrayList<>();

        students.add(new Student(101, "Aman"));
        students.add(new Student(102, "Riya"));
        students.add(new Student(103, "Rahul"));

        Map<Integer, String> map
            = students.stream().collect(Collectors.toMap(
                Student::getId, Student::getName));

        System.out.println("List converted to Map: " + map);
    }
}

Output
List converted to Map: {101=Aman, 102=Riya, 103=Rahul}

Explanation: Student::getId is used to create the Map keys, while Student::getName provides the corresponding values.

Note: Collectors.toMap() throws IllegalStateException if two elements produce the same key. A merge function can be supplied when duplicate keys are possible.

3. Using Collectors.groupingBy()

groupingBy() is useful when multiple List elements have the same key. Instead of replacing an existing value, it groups all matching values into a List.

Approach:

  1. Create a List containing objects with duplicate keys.
  2. Convert the List into a Stream.
  3. Group the objects using Collectors.groupingBy().
  4. Use Collectors.mapping() to store only the required values.
Java
import java.util.*;
import java.util.stream.Collectors;

class Student {
    private int id;
    private String name;

    Student(int id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public int getId() { return id; }

    public String getName() { return name; }
}

class ListToMultiMap {
    public static void main(String[] args)
    {

        List<Student> students = new ArrayList<>();

        students.add(new Student(1, "Aman"));
        students.add(new Student(1, "Riya"));
        students.add(new Student(2, "Rahul"));
        students.add(new Student(2, "Neha"));

        Map<Integer, List<String> > map = students.stream().collect(Collectors.groupingBy(Student::getId,
                    Collectors.mapping(
                        Student::getName,
                        Collectors.toList())));

        System.out.println("Grouped Map: " + map);
    }
}

Output
Grouped Map: {1=[Aman, Riya], 2=[Rahul, Neha]}

Explanation: Students having the same id are grouped together. Therefore, each Map key contains a List of names instead of a single value.

Note: HashMap does not guarantee the order of its elements. Therefore, the order of entries in the displayed Map may vary between executions.

Comment