Streams Java8: Convert Empoyee Object to Map

 import java.util.Arrays;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class EmployeeToMap {
private Integer id;
private String name;

public EmployeeToMap(Integer id, String name) {
this.id = id;
this.name = name;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public static void main(String[] args) {
List<EmployeeToMap> employees =
Arrays.asList(new EmployeeToMap(1,"ramu"),
new EmployeeToMap(2,"rajesh"));

Map<Integer, EmployeeToMap> params=employees.stream().collect(Collectors.toMap(e-> e.getId(),
Function.identity(),(o1, o2) -> o1, LinkedHashMap::new));
}
}

Comments