gpt4 book ai didi

java - 可选的 IfPresent 和其他选择

转载 作者:行者123 更新时间:2023-11-29 04:51:46 29 4
gpt4 key购买 nike

我如何制作将类型设置为 BYTESSECONDS 的单个代码。在这里,我设置了一个默认的 BYTES,然后使用 setTypeAsSecondsIfCcTimeIsValid 函数调用将其覆盖为 SECONDS

    } else if (mscc.getRsu().isPresent()) {
type = Type.BYTES;
mscc.getRsu().get().getCcTime().ifPresent(this::setTypeAsSecondsIfCcTimeIsValid);
}

最佳答案

像这样的东西应该可以工作:

type = mscc.getRsu().flatMap(rsu -> rsu.getCcTime())
.filter(ccTime -> isCcTimeValid(ccTime))
.map(ccTime -> Type.BYTES).orElse(Type.SECONDS);

但是我会改用三元:

type = mscc.getRsu().flatMap(rsu -> rsu.getCcTime())
.filter(ccTime -> isCcTimeValid(ccTime))
.isPresent() ? Type.BYTES : Type.SECONDS;

此代码假定您有 isCcTimeValid 方法。

关于java - 可选的 IfPresent 和其他选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194757/

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