I am trying to convert the below nested for loop into hashmap using java stream but i got struck in the collector step. Could you please help?
Existing code:
private static HashMap<String, Long> getOutput(List<Employee> eList) {
HashMap<String, Long> outputList = new HashMap<>();
for (Employee employee : eList) {
List<Department> departmentList = employee.getDepartmentList();
for (Department department : departmentList) {
if (department.getType().equals(DepartmentType.SCIENCE)) {
outputList.put(employee.getName(),department.getDepartmentId()));
}
}
}
return outputList;
}
So far i tried:
private static HashMap<String, Long> getOutput(List<Employee> eList) {
return eList.stream()
.flatMap(emp -> emp.getDepartmentList().stream()
.filter(dept -> dept.getType().equals(DepartmentType.SCIENCE))
.collect(HashMap::new, ???)
}