How to convert List to Map in Java 8 | Java 8 List to Map | Online Tutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about the conversion of List to Map in Java 8.
We can convert the Java List<?> of objects into Map<k,v> in Java 8 via the Collectors class. The Collectors class provides a method called toMap() to convert List to Map.
Collectors.toMap() :
toMap() is a static method. It returns the Collector instance containing results (Map) of provided mapping functions as input parameters.
class Department {
    private int deptId;
    private String deptName;

    public Department(int deptId, String deptName) {
        super();
        this.deptId = deptId;
        this.deptName = deptName;
    }

    public int getDeptId() {
        return deptId;
    }
    public String getDeptName() {
        return deptName;
    }

}
Here we are going to convert a list of Department objects into Map.
package com.onlinetutorialspoint.java8;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Java8_ListToMap {

    public static void main(String[] args) {
        List<Department> deptList = new ArrayList<Department>();    
        deptList.add(new Department(1, "IT"));
        deptList.add(new Department(2, "HR"));
        deptList.add(new Department(3, "Management"));
        deptList.add(new Department(4, "Development"));
        deptList.add(new Department(5, "Recruitment"));
        Map<Integer, String> deptMap = deptList.stream().collect(
                Collectors.toMap(Department::getDeptId, Department::getDeptName));
        
        deptMap.forEach((k,v)->System.out.println("DeptId (" + k + ") Name :" + v));
    }
    
}
Output :
DeptId (1) Name :IT
DeptId (2) Name :HR
DeptId (3) Name :Management
DeptId (4) Name :Development
DeptId (5) Name :Recruitment
The above example runs well if the map doesn’t contain any null key or values. If anyone of the entry is null, the toMap() method throws NullPointerException.
List<Department> deptList = new ArrayList<Department>();    
        deptList.add(new Department(1, "IT"));
        deptList.add(new Department(2, "HR"));
        deptList.add(new Department(3, null));
        deptList.add(new Department(4, "Development"));
        deptList.add(new Department(5, "Recruitment"));
        Map<Integer, String> deptMap = deptList.stream().collect(Collectors.toMap(Department::getDeptId, Department::getDeptName));
        deptMap.forEach((k,v)->System.out.println("DeptId (" + k + ") Name :" + v));

The above code will generate NullPointerException like below:
Exception in thread "main" java.lang.NullPointerException
at java.util.stream.Collectors.lambda$toMap$58(Unknown Source)
at java.util.stream.ReduceOps$3ReducingSink.accept(Unknown Source)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.AbstractPipeline.copyInto(Unknown Source)

You can also referto know how to resolve NullPointerException in Collectors.toMap().
Happy Learning 🙂

Resolve NullPointerException in Collectors.toMap
Java 8 how to remove duplicates from list
Java 8 groupingBy Example
How to Filter a Map in Java 8
Java 8 How to convert Stream to List
How to calculate Employees Salaries Java 8 summingInt
Java 8 How to Convert List to String comma separated values
How to Filter null values from Java8 Stream
How to convert Java Map to JSON
Java 8 Stream Filter Example with Objects
How to convert JSON to Java Map Object
How to sort a Map using Java8
Java how to convert ArrayList to Array Example
Spring Collection Map Dependency Example
How to Convert Iterable to Stream Java 8

Resolve NullPointerException in Collectors.toMap
Java 8 how to remove duplicates from list
Java 8 groupingBy Example
How to Filter a Map in Java 8
Java 8 How to convert Stream to List
How to calculate Employees Salaries Java 8 summingInt
Java 8 How to Convert List to String comma separated values
How to Filter null values from Java8 Stream
How to convert Java Map to JSON
Java 8 Stream Filter Example with Objects
How to convert JSON to Java Map Object
How to sort a Map using Java8
Java how to convert ArrayList to Array Example
Spring Collection Map Dependency Example
How to Convert Iterable to Stream Java 8
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String