1
我很困惑于orElse
方法的可选。 我用下面的代码,虽然可选的值是本它调用orElse
情况下每次:Java 8可选的orElse,而isPresent
Optional<NotificationSettings> ons = userProfileDao.loadNotificationSettingsByTransportType(type);
NotificationSettings notificationSettings = ons.orElse(createNotificationSettings(profile, type));
如果我重写代码下文中,正确的路径(ifPresent
)被选择:
Optional<NotificationSettings> ons = userProfileDao.loadNotificationSettingsByTransportType(type);
NotificationSettings notificationSettings = ons.isPresent() ? ons.get() : createNotificationSettings(profile, type);
我以为orElse
就像我在第二种情况下的例子一样。我错过了什么?