Java 8 Convert List to Map Example

Converting List to Map is a common task. In this example, we’ll cover converting List to Map before Java 8 and after Java 8.

We’ll assume that each element of the List has an identifier which will be used as a key in the resulting Map.

Employee

Firstly, let’s model the element:
class Employee {

    private Long id;
    private String name;

    public Employee(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

The id field is unique, hence we can make it the key.

Let’s start converting with the traditional way.

Before Java 8

Evidently, we can convert a List to a Map using core Java methods:
public Map < Long, Employee > convertListBeforeJava8(List < Employee > list) {

        Map < Long, Employee > map = new HashMap < > ();

        for (Employee animal: list) {
            map.put(animal.getId(), animal);
        }
        return map;
    }

With Java 8

Starting with Java 8 we can convert a List into a Map using streams and Collectors:
public Map < Long, Employee > convertListAfterJava8(List < Employee > list) {
        Map < Long, Employee > map = list.stream().collect(Collectors.toMap(Employee::getId, animal - > animal));
        return map;
    }

Complete Example

package net.javaguides.hibernate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ConvertListToMapService {
    public Map < Long, Employee > convertListBeforeJava8(List < Employee > list) {

        Map < Long, Employee > map = new HashMap < > ();

        for (Employee animal: list) {
            map.put(animal.getId(), animal);
        }
        return map;
    }

    public Map < Long, Employee > convertListAfterJava8(List < Employee > list) {
        Map < Long, Employee > map = list.stream().collect(Collectors.toMap(Employee::getId, animal - > animal));
        return map;
    }

    public static void main(String[] args) {

        ConvertListToMapService convertListToMapService = new ConvertListToMapService();
        List < Employee > list = new ArrayList < > ();
        list.add(new Employee(100 L, "Ramesh"));
        list.add(new Employee(101 L, "Tom"));
        list.add(new Employee(102 L, "Tony"));

        Map < Long, Employee > map = convertListToMapService.convertListBeforeJava8(list);
        System.out.println(map);

        Map < Long, Employee > map1 = convertListToMapService.convertListAfterJava8(list);
        System.out.println(map1);
    }
}

class Employee {

    private Long id;
    private String name;

    public Employee(Long id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Output
{100=net.javaguides.hibernate.Employee@7852e922, 101=net.javaguides.hibernate.Employee@4e25154f,
 102=net.javaguides.hibernate.Employee@70dea4e}
{100=net.javaguides.hibernate.Employee@7852e922, 101=net.javaguides.hibernate.Employee@4e25154f, 
102=net.javaguides.hibernate.Employee@70dea4e}

Comments