Flatmapping Optional of collection

Viewed 2102

So I have variable Optional<Collection<Student>> and I would like to use .findFirst() lambda method to find Student address.

The way I am doing it right now is this

Optional<Collection<Student>> students = ...;
return students.map(s -> s.stream()
                            .filter(...)
                            .findFirst())
                    .orElse(Optional.empty());

Is there A better way to do this so I wouldn't have stream inside map?

1 Answers
Related