Getting a Stream of objects based on value of other member object

devd :

I have a Java class of type Project which contains Map of type Process and class Process subsequently contains Map of type Activity.

public class Project {
    private String name;
    private Path path;
    private Map<String, Process> processes;
    //getters and setters
    }

public class Process {
    private String name;
    private String path;
    private Map<String, Activity> activities;
    //getters and setters
    }

public class Activity {
    private String name;
    private String type;
    //getters and setters
    }

I'm trying to write a function which will return a Stream of Process or Process.path where the Process contains at least one activity having type="starter" i.e. Activity.type="starter".

I came up with the below code but it returns a Stream<Activity> whereas what I'm looking for is a Stream<Process> or Stream<String> containing the path.

project.processes.values().stream()
    .flatMap(process -> process.getActivities().values().stream()
            .filter(activity -> "starter".equals(activity.getType())));

What is the most efficient way of doing this?

Sweeper :

Don't use flatMap if you want a Stream<Process> because flatMap will "destroy" information about the processes.

You should create a "nested" stream:

project.getProcesses().values().stream()
        .filter(x -> 
                x.getActivities().values().stream().anyMatch(
                        y -> "starter".equals(y.getType())
                )
        );

Now it's a Stream<Process>.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144944&siteId=1