gpt4 book ai didi

java - ifPresent 返回一些东西 orElse

转载 作者:行者123 更新时间:2023-11-30 05:44:52 25 4
gpt4 key购买 nike

我需要一些关于以下代码片段的帮助。如果raiseWorkQueueTask可以找到任何null != previousPeriod.getOldId(),则它应该返回通知riseWorkQueue 的 >outcomeCode 是“L3”....但我在

处收到错误
*.ifPresent(()->proceedWorkQueue(ip)).orElse(null);*

我收到一条错误消息“无法推断功能接口(interface)” ..我还尝试了其他变体...

.ifPresent(()->proceedWorkQueue(ip).orElse(null));
.ifPresent(proceedWorkQueue(ip).orElse(null));
<小时/>
private Optional<Notification> riseWorkQueueTask(IP ip) {
return ip.getPreviousPeriods().stream()
.filter(previousPeriod -> null != previousPeriod.getOldId())
.findAny()
.ifPresent(() -> proceedWorkQueue(ip))
.orElse(null);
}

private Optional<Notification> proceedWorkQueue(Ip ip) {
EntryParametersDTO entryParametersDTO = new EntryParametersDTO();
Insurance insurance = ip.getInsurance();
//.....
return queueClient.riseWorkQueue(entryParametersDTO)
.filter(w -> w.getStateDTO() != null)
.filter(w -> StateConstants.L3.equalsIgnoreCase(
w.getStateDTO().getOutcomeCode()))
.flatMap(flag -> retrieveNotificationFromDB(insurance));
}

最佳答案

您可能正在寻找 map :

private Optional<Notification> riseWorkQueueTask(IP ip) {
return ip.getPreviousPeriods().stream()
.filter(previousPeriod -> null != previousPeriod.getOldId())
.findAny()
.map(i -> proceedWorkQueue(i).orElse(null));
// orElse if 'proceedWorkQueue' returns empty Optional
}

或者简单地使用flatMap作为:

private Optional<Notification> riseWorkQueueTask(IP ip) {
return ip.getPreviousPeriods().stream()
.filter(previousPeriod -> null != previousPeriod.getOldId())
.findAny()
.flatMap(i -> proceedWorkQueue(i));
}

关于java - ifPresent 返回一些东西 orElse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55043178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com