I have an Optional
object that contains a list. I want to map each object in this list to another list, and return the resulting list.
That is:
public List<Bar> get(int id) {
Optional<Foo> optfoo = dao.getById(id);
return optfoo.map(foo -> foo.getBazList.stream().map(baz -> baz.getBar()))
}
Is there a clean way of doing that without having streams within streams?
I think that flatMap
might be the solution but I can't figure out how to use it here.
>`. The `List` could just be empty, so wrapping it in an `Optional` (which, itself can be thought of as a special kind of list that can only have zero or one items) serves no purpose.
– David Conrad Jun 14 '18 at 07:04