Hi guys, I'm a little stuck with something here.
Basically I have a a method which, given a List of Students will have to filter out those who are younger than 60yrs old and should return a List of Students who are over 60yrs old but I want this to be a list of Optionals rather than just a List of Students. THe difficulty is that I can't find a way to return a list of optionals like List<Optional>. I can return an optional if I use things like findFirst() or findAny()
return studentList.stream().filter(x -> x.getAge() > 60).findAny();
or i could return a simple list of students
return studentList.stream().filter(x -> x.getAge() > 60).collect(Collectors.toList());
but I want all the Students over 60 in a list of Optional.
Any idea how that can be done if it can be done that is?
thanks
Violet_82
89
Posting Whiz in Training
Recommended Answers
Jump to Postmap(s -> Optional.of(s)) is transforming those objects into Optionals, correct?
Yes, but more accuratel:
.map
creates a new Stream from the values returned by the lambda.There are probably other ways to do it, but this is a really obvious and easy-to-read solution.
All 4 Replies
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Violet_82
89
Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.