I have below existing code which converting one object to another -
for(Department dept : company.getDepartments()) {
if(!isEmpty(dept.getEmployees())) {
for(Employee emp : dept.getEmployees()) {
try {
employyeV2List.add(new EmployeeV2(emp.getId(), emp.getFirstName(),..., dept.getId()));
} catch (ParseException e) {
//error logger
}
}
}
}
I want add java 8 stream api here instead of two for loops but if you see in try block there is dept.getId() which I can not access in stream API. I tried below -
List<Employee> employees = company.getDepartment().stream().map(x -> x.getEmployees())
.flatMap(x -> x.stream()).collect(Collectors.toList());
List<EmployeeV2> employeeV2List = employees.stream().map(x -> getEmployeeV2(x)).collect(Collectors.toList());
Here in getEmployeeV2() I am creating EmployeeV2 object. But I not sure how I can pass Department to here so I can access department id.