collectorConfiguration = Optional.ofNullable(recapPlacement.getPnetCode())
.map(pnetCode -> Optional.ofNullable(collectorConfigurations.stream()
.filter(c -> pnetCode.equals(c.getPnetCode()))
.collect(Collectors.toList())
.stream().filter(Objects::nonNull).findFirst())
.orElse(unknownExternalCollector))
.orElse(unknownExternalCollector);
在上面的代码中
1)如果pnetCode为null,我想返回unknownExternalCollector
或
2)我想迭代collectorConfigurations
并找到至少一个
如果列表中没有任何元素,则使用pnetCode进行记录
匹配pnetCode,然后我想返回unknownExternalCollector
。
我收到编译错误,说
orElse
(java.util.Optional<com.capitalone.omega.collectorservice.domain.CollectorConfiguration>)
in Optional cannot be applied om.capitalone.omega.collectorservice.domain.CollectorConfiguration
答案 0 :(得分:4)
您可以将其简化如下(无需收集然后流式传输和调用findFirst
。您可以在此处使用flatMap
)
collectorConfiguration = Optional.ofNullable(recapPlacement.getPnetCode())
.flatMap(pnetCode -> collectorConfigurations.stream()
.filter(c -> pnetCode.equals(c.getPnetCode()))
.filter(Objects::nonNull)
.findFirst())
.orElse(unknownExternalCollector);