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
就像我在第二種情況下的例子一樣。我錯過了什麼?