Monday 6 April 2020

Convert List to a Map

Approach 1: By traversing each element of the list.
Map<Integer, Employee> map1 = new HashMap<>();
for (Employee emp : emps) {
         map1.put(emp.getId(), emp);
}

Approach 2: Using stream() method
Map<Integer, Employee> map2 = emps.stream()
                  .collect(Collectors.toMap(Employee::getId, emp -> emp, (empObj1, empObj2) -> {
                           return empObj1;
                  }));

Approach 3: Using parallelStream() method
Map<Integer, Employee> map3 = emps.parallelStream()
                  .collect(Collectors.toMap(Employee::getId, item -> item, (empObj1, empObj2) -> {
                           return empObj1;
                  }));

Find the below working application.

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.sample.app.model.Employee;

public class App {

 private static void printMap(Map<Integer, Employee> map) {
  Set<Integer> set = map.keySet();

  for (int key : set) {
   System.out.println(map.get(key));
  }

  System.out.println("-----------------------------");

 }

 public static void main(String args[]) {
  Employee emp1 = new Employee(1, "Krishna", "Gurram");
  Employee emp2 = new Employee(2, "Narasimha", "Rao");
  Employee emp3 = new Employee(3, "Ram", "Pothineni");
  Employee emp4 = new Employee(4, "Jaidepp", "Geera");
  Employee emp5 = new Employee(5, "Joel", "Chelli");

  List<Employee> emps = Arrays.asList(emp1, emp2, emp3, emp4, emp5);

  Map<Integer, Employee> map1 = new HashMap<>();
  for (Employee emp : emps) {
   map1.put(emp.getId(), emp);
  }

  Map<Integer, Employee> map2 = emps.stream()
    .collect(Collectors.toMap(Employee::getId, emp -> emp, (empObj1, empObj2) -> {
     return empObj1;
    }));

  Map<Integer, Employee> map3 = emps.parallelStream()
    .collect(Collectors.toMap(Employee::getId, item -> item, (empObj1, empObj2) -> {
     return empObj1;
    }));

  printMap(map1);
  printMap(map2);
  printMap(map3);
 }

}

Output

Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Narasimha, lastName=Rao]
Employee [id=3, firstName=Ram, lastName=Pothineni]
Employee [id=4, firstName=Jaidepp, lastName=Geera]
Employee [id=5, firstName=Joel, lastName=Chelli]
-----------------------------
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Narasimha, lastName=Rao]
Employee [id=3, firstName=Ram, lastName=Pothineni]
Employee [id=4, firstName=Jaidepp, lastName=Geera]
Employee [id=5, firstName=Joel, lastName=Chelli]
-----------------------------
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Narasimha, lastName=Rao]
Employee [id=3, firstName=Ram, lastName=Pothineni]
Employee [id=4, firstName=Jaidepp, lastName=Geera]
Employee [id=5, firstName=Joel, lastName=Chelli]
-----------------------------


You may like

No comments:

Post a Comment