How to Convert List to Map in Java

As a developer many times we need to Convert List to Map.There are many ways to do it.
The following are the some way to achive it.

1). Using by object of list

In this way we have to get List to be converted into Map. Then Iterate through the items in the list and add each of them into the Map.

package com.javacodestuffs.core.collections; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.HashMap; class Employee { private Integer id; private String name; public Employee(Integer id, String name) { this.id = id; this.name = name; } public Integer getId() { return id; } public String getName() { return name; } } public class ListToMap { public static void main(String[] args) { List <Employee> empList = new ArrayList <Employee>(); empList.add(new Employee(1001, "Anna")); empList.add(new Employee(1002, "Rashmika")); empList.add(new Employee(1003, "Kedarnath")); empList.add(new Employee(1004, "Surya")); Map <Integer,String> map = new HashMap <Integer,String>(); for (Employee emp: empList) { map.put(emp.getId(), emp.getName()); } System.out.println("Employee Map is : " + map); } } output: Employee Map is : {1001=Anna, 1002=Rashmika, 1003=Kedarnath, 1004=Surya}

2). Using Java8

In Java 8, you can convert a List to Map in one line using the stream() and Collectors.toMap() utility methods.

public static Map getMapFromList(List empList) { Map employeeMap = empList.stream().
collect(Collectors.toMap(Employee :: getId, Employee :: getName)); return employeeMap; }

Handle Duplicate Keys while Using Java8

Collectors.toMap() fails when converting a list with duplicate items.
We need to pass a third argument that informs the toMap() which value to consider when facing duplicate items.

public static Map<Integer,String> getMapNoDuplicates(List<Employee> empList) { Map<Integer, String> employeeMap = empList.stream().collect(Collectors.
toMap(Employee :: getId, Employee :: getName , (oldValue, newValue) -> oldValue)); return employeeMap; }

Preserve List Order while Using Java8

We need to pass another parameter to Collectors.toMap() which decides what type of map to use.

public static Map<Integer,String> getMapPreserveOrders(List<Employee> empList) { Map<Integer, String> employeeMap = empList.stream().collect(Collectors.
toMap(Employee :: getId, Employee :: getName , (oldValue, newValue) -> oldValue,LinkedHashMap::new)); return employeeMap; }

Related Questions/Posts to How to Convert List to Map in Java

How to convert list to map in java 8?
How to convert map to set in java?
How to convert map to list in java 8?
How to convert map to string in java?
How to create map object in java?
How to convert map to object in java?
How to instantiate a map in java?
How to convert properties to map in java?
How to retrieve map elements in java?
How to check map contains key in java?

In this article, we have seen the  How to Convert List to Map in Java with examples. All source code in the article can be found in the GitHub repository.