Java 8 - Convert List to Map Examples

In this tutorial we will take a look at how to convert a Java Collection List to a Map, there are multiple ways in which you can do so, we will take a look at using Streams,


Let us first create our Class called Student,

public class Student {

    int studentId;
    String studentName;
    int studentAge;

    public Student(int studentId, String studentName, int studentAge) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.studentAge = studentAge;

    public int getStudentId() {
        return studentId;

    public void setStudentId(int studentId) {
        this.studentId = studentId;

    public String getStudentName() {
        return studentName;

    public void setStudentName(String studentName) {
        this.studentName = studentName;

    public int getStudentAge() {
        return studentAge;

    public void setStudentAge(int studentAge) {
        this.studentAge = studentAge;

    public String toString() {
        return "Student{" +
                "studentId=" + studentId +
                ", studentName='" + studentName + '\'' +
                ", studentAge=" + studentAge +
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

 * Java 8 Example to Convert a 
 * Collection List Object into 
 * a Map
 * Author:
 * Date: 03-Apr-2022
 * Version: 1.0
public class ListToMapExample {

    public static void main(String[] args) {

        //Step 1: Create some Student Objects
        Student student1 = new Student(1, "Sam", 19);
        Student student2 = new Student(2, "Mike", 18);
        Student student3 = new Student(3, "Alex", 21);

        //Step 2: Add them to a List
        List studentList = new ArrayList<>();

        //Step 3: Converting Student List to a Map using Stream
        Map<Integer, Student> studentMap = (Map<Integer, Student>)
                .collect(Collectors.toMap(Student::getStudentId, Function.identity()));

       //Step 4: Let's print the results
        for (Map.Entry<Integer, Student> entry : studentMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());

Java 8 - Convert List to Map Example Output
Java 8 - Convert List to Map Example Output

Note that the key that you identify from the List for the Map should be unique or else you will get java.lang.IllegalStateException: Duplicate key

Exception in thread "main" java.lang.IllegalStateException: Duplicate key 2 (attempted merging values Student{studentId=2, studentName='Mike', studentAge=18} and Student{studentId=2, studentName='Mike', studentAge=18})
	at java.base/
	at java.base/$uniqKeysMapAccumulator$1(
	at java.base/$3ReducingSink.accept(
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(
	at java.base/
	at java.base/
	at java.base/$ReduceOp.evaluateSequential(
	at java.base/
	at java.base/
	at ListToMapExample.main(

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail:

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap