I have an optionalIdList
( Optional<Set<String>> optionalIdList
) that I want to iterate over, and then convert back to Optional<Set<String>>
. My code thus far is like this:
optionalIdList
.map(
idList ->
idList.stream()
.map(
id ->
filterIds(
partnerIds
)))
.flatMap(streamOfLists -> streamOfLists.map(item -> item.orElseGet(ImmutableSet::of)));
The filterIds
list returns Optional<Set<String>>
However with my current solution I get the following error:
Required type:
Optional<Set<String>>
Provided:
Optional<Object>
Is there a nice way to do what I want to do?
EDIT: For some reason I assumed filterIds
returns a single String
, and not an Optional<Set<String>>
as you wrote it does.
Here's the updated answer:
Optional<Set<String>> output =
optionalIdList.map(idList -> idList.stream()
.map(id -> filterIds(partnerIds))
.filter (Optional::isPresent)
.map (Optional::get)
.flatMap (Set::stream)
.collect(Collectors.toSet()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.