有没有更好的方法:
DateTime firstRecordingDate = null;
if (firstScheduleRecordWeekCompendium.isPresent()) {
firstRecordingDate = firstScheduleRecordWeekCompendium.get().getStartRecord();
}
DateTime lastRecordingDate = null;
if (firstScheduleRecordWeekCompendium.isPresent()) {
lastRecordingDate = firstScheduleRecordWeekCompendium.get().getStartRecord();
}
答案 0 :(得分:3)
如果您将DateTime
变量的类型更改为Optional<DateTime>
,则可以使用map
来避免使用if语句:
Optional<DateTime> firstRecordingDate = firstScheduleRecordWeekCompendium.map(ClassName::getStartRecord);
其中“ClassName”应替换为firstScheduleRecordWeekCompendium
的类型。
答案 1 :(得分:2)