This article contains Java 8 List to Map using stream example like List to Map using toMap, List to Map using parallel Stream.

Example 1: List to Map using toMap

In this example, we are converting List to Map using Collectors.toMap() method. toMap method requires two parameters first one consider as Map Key and second parameter considers as map value.

java.util.List<Employee> employeeList = new ArrayList<>();
       employeeList.add(new Employee(1, "Jone"));
       employeeList.add(new Employee(2, "Shone"));
       java.util.Map<String, Employee> employeeMap = employeeList.stream().
               collect(Collectors.toMap(employee -> employee.getName(), employee -> employee));
       employeeMap.forEach((id, employee) -> {
           System.out.println("Key: " + id + ", Value:" + employee.toString());
       });

Output:

Key: Shone, Value:2-Shone
Key: Jone, Value:1-Jone

Example 2 : List to Map using toMap + Function.identity()

In example is same as above but here Function.identity() method is used. Function.identity function that always returns its input argument.

java.util.List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee(1, "Jone"));
        employeeList.add(new Employee(2, "Shone"));
        java.util.Map<String, Employee> employeeMap = employeeList.stream().
                collect(Collectors.toMap(employee -> employee.getName(), Function.identity()));
        employeeMap.forEach((id, employee) -> {
            System.out.println("Key: " + id + ", Value:" + employee.toString());
        });

Output:

Key: Shone, Value:2-Shone
Key: Jone, Value:1-Jone

 

Example 3: List to Map using supplier + accumulator + combiner

In this solution, I have used stream.collect method using supplier, accumulator, and combiner. Here combiner method is empty because as per java documentation combiner only call with the parallel stream.

java.util.List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee(1, "Jone"));
        employeeList.add(new Employee(2, "Shone"));
        java.util.Map<String, Employee> employeeMap = employeeList.stream().
                collect(HashMap<String, Employee>::new, (map, employee) -> {
                    map.put(employee.getName(), employee);
                }, (map, employee) -> {                                // here is function is empty because combiner called only with parallel stream
                });
        employeeMap.forEach((id, employee) -> {
            System.out.println("Key: " + id + ", Value:" + employee.toString());
        });

Output:

Key: Shone, Value:2-Shone
Key: Jone, Value:1-Jone

Example 4: List to Map using parallelStream

If the parallel stream is more feet to your requirement than List to Map using the parallel stream as below:

java.util.List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee(1, "Jone"));
        employeeList.add(new Employee(2, "Shone"));
        java.util.Map<String, Employee> employeeMap = employeeList.parallelStream().
                collect(HashMap<String, Employee>::new, (map, employee) -> {
                    map.put(employee.getName(), employee);
                }, (map, employeeMp) -> {     // it will combine parallel map result
                    map.putAll(employeeMp);
                });
        employeeMap.forEach((id, employee) -> {
            System.out.println("Key: " + id + ", Value:" + employee.toString());
        });

Output:

Key: Shone, Value:2-Shone
Key: Jone, Value:1-Jone

Employee.java

class Employee {
    int no;
    String name;
    Employee(int no, String name) {
        this.no = no;
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    @Override
    public String toString() {
        return no + "-" + name;
    }
}

References:

http://stackoverflow.com/questions/20363719/java-8-listv-into-mapk-v

 

Was this post helpful?

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *