5

I'm new to Java 8 and the Stream API.

If I have a list of Employee objects:

List<Employee> employees;

public class Employee {
    private String name;
    private List<Project> involvedInProjects;
}

public class Project {
    private int projectId;
}

I want to filter on employees beeing involved in certain projects, how would I go about doing this with the stream API in java 8?

Would it be easier if I had a Map where the key was an unique employeeID, instead of a List?

0

2 Answers 2

14

So you can get access to the nested list inside of a stream operation and then work with that. In this case we can use a nested stream as our predicate for the filter

employees.stream().filter(
employee -> employee.involvedInProjects.stream()
.anyMatch(proj -> proj.projectId == myTargetId ))

This will give you a stream of all of the employees that have at least one project matching you targetId. From here you can operate further on the stream or collect the stream into a list with .collect(Collectors.toList())

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

Comments

2

If you don't mind modifying the list in place, you can use removeIf and the predicate you will give as parameter will check if the projects where an employee is involved does not match the given id(s).

For instance,

employees.removeIf(e -> e.getInvolvedInProjects().stream().anyMatch(p -> p.getProjectId() == someId));

If the list does not support the removal of elements, grab the stream from the employees list and filter it with the opposite predicate (in this case you could use .noneMatch(p -> p.getProjectId() == someId)) as in removeIf and collect the result using Collectors.toList().

2 Comments

When using streams and filter, you need the opposite predicate as in removeIf. Besides that, it’s preferable to get the actual Project instance first and do a simple getInvolvedInProjects() .contains(project) test (assuming that Projects are either canonical or have a proper equals method, but everything else would be a questionable design)…
@Holger Thanks, don't know what's wrong with me today! Totally agree with your last point...

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.