Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8 Java 8可选orElse,而iPresent_Java 8_Optional - Fatal编程技术网

Java 8 Java 8可选orElse,而iPresent

Java 8 Java 8可选orElse,而iPresent,java-8,optional,Java 8,Optional,我对可选的orElse方法感到非常困惑。 我使用了以下代码,尽管存在可选值,但每次都调用orElse案例: Optional<NotificationSettings> ons = userProfileDao.loadNotificationSettingsByTransportType(type); NotificationSettings notificationSettings = ons.orElse(createNotificationSettings(profile,

我对可选的
orElse
方法感到非常困惑。 我使用了以下代码,尽管存在可选值,但每次都调用
orElse
案例:

Optional<NotificationSettings> ons = userProfileDao.loadNotificationSettingsByTransportType(type);
NotificationSettings notificationSettings = ons.orElse(createNotificationSettings(profile, type));

我认为,
orElse
与我在第二种情况下的示例一样。我遗漏了什么?

为了避免评估替代值,请使用
orElseGet

NotificationSettings notificationSettings = 
    ons.orElseGet(() -> createNotificationSettings(profile, type));

没有魔法。如果调用像
orElse
这样的方法,它的所有参数都会得到热切的求值
OrelGet
通过接收要延迟评估的供应商来绕过它。

为了避免评估替代值,请使用
OrelGet

NotificationSettings notificationSettings = 
    ons.orElseGet(() -> createNotificationSettings(profile, type));
没有魔法。如果调用像
orElse
这样的方法,它的所有参数都会得到热切的求值
orElseGet
通过接收要延迟评估的
供应商来绕过它