我正在尝试在 orElse
中使用 Stream
并且难以理解错误。
collectorConfiguration = Optional.ofNullable(recapPlacement.getAttId())
.map(attId -> Optional.ofNullable(processorEngine.buildFrimFromAttId(attId))
.orElseThrow( () -> new OmegaException("UnableToFirmByAttId", recapPlacement.getAttId())))
.orElse( () -> Optional.ofNullable(collectorConfigurations.stream() //getting error here
.filter(cc -> recapPlacement.getPnetCode().equals(cc.getPnetCode()))
.filter(Objects::nonNull)
.findFirst())
.orElseThrow( () -> new OmegaException("CollectorCouldNotMapForPnetCode", recapPlacement.getPnetCode()))
);
在上面的代码中,我正在尝试总体
如果
attId
不为 null,则获取collectorConfig
如果
<attId
不为 null 并且未找到该attId
的collectorConfig
那么我将抛出异常如果
attId
为空,那么我将使用pnet
代码通过流式传输collectConfigurations
获取collectConfig
列表如果
pnetCode
没有找到collectConfig
,那么我将抛出异常
它在 orElse
block 中给出编译错误“Lambda 表达式的目标类型必须是一个接口(interface)
”。
最佳答案
你可能想替换
.orElse( () -> Optional.ofNullable(collectorConfigurations.stream() //getting error here
与Optional.orElseGet
期望 Supplier
为:
.orElseGet( () -> Optional.ofNullable(collectorConfigurations.stream() ...
除了上述之外,你不应该需要供应商中的Optional.ofNullable
.orElseGet( () -> collectorConfigurations.stream()
.filter(cc -> recapPlacement.getPnetCode().equals(cc.getPnetCode()))
.filter(Objects::nonNull) //non-null filtered
.findFirst()) // optional
.orElseThrow( () -> new OmegaException("CollectorCouldNotMapForPnet...
关于java - Stream in orElse of optional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416163/