0

My EmployeeDeatils Object has List of other object inside (employeeData). I am trying to fetch that with some mapping with id

But not sure what I am doing wrong

     private List<EmployeeData> getEmployeeMessage(List<EmployeeDetails> employeeDetailList, Employee employee) {

    return employeeDetailList.stream()
        .filter(employeeDetail -> employeeDetail.getEmployeeId() == employee.getEmployeeId())
        .map(employeeDetail -> employeeDetail.getEmployeeData())
        .collect(Collectors.toList);

}
2
  • 3
    I guess your problem is a compiler error, so I suggest to pay attention to the compiler error and what it says. Otherwise, include the problem in your question. Commented Dec 3, 2020 at 20:46
  • Mostly understanding about map versus flatMap as it sounds like. Commented Dec 4, 2020 at 2:46

1 Answer 1

1

Assuming that your EmployeeDetails class has List<EmployeeData> getEmployeeData() field, you can fetch these using:

List<EmployeeData> getEmployeeMessage(List<EmployeeDetails> employeeDetailList, Employee employee) {

        return employeeDetailList.stream()
                .filter(employeeDetail -> employeeDetail.getEmployeeId() == employee.getEmployeeId())
                .flatMap(employeeDetail -> employeeDetail.getEmployeeData().stream())
                .collect(Collectors.toList());

    }

As @Naman correctly pointed out, you need to pay attention on flatMap vs map operations.

In this case, a flatMap is used because you need a mapping operation for each element of your list (returned by getEmployeeData()).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.