Reputation: 284
I have this situation:
Class Employee{
//some attributes
List<String> idContract
//getter, setter
}
And I need to retrieve a List idContracts from a list using java 8 stream.
I was trying something like that:
lst.stream().filter(o->!o.getLstDipFuoriSoglia().isEmpty())
.map(ResultOdg::getLstDipFuoriSoglia)
.collect(Collectors.toList());
but that, of course, returns a List< List < String >>, so how can I achieve that goal?
Thanks for your answers
Upvotes: 0
Views: 343
Reputation: 40048
Just add flatMap
operation for converting List
to Stream
lst.stream().filter(o->!o.getLstDipFuoriSoglia().isEmpty)
.map(ResultOdg::getLstDipFuoriSoglia)
.flatMap(List::stream)
.collect(Collectors.toList());
Or you can have one flatMap
operation
lst.stream().filter(o->!o.getLstDipFuoriSoglia().isEmpty)
.flatMap(list->list.getLstDipFuoriSoglia().stream())
.collect(Collectors.toList());
Upvotes: 7