我有以下lambda表達式,並且如果在bonusScheduleDurationContainers
非空時正常工作。如果它是空的,我得到一個NoSuchElementException
。我如何在lambda表達式中檢查這個?Java Lambda - 檢查一個ArrayList到Stream是否爲空
final List<ScheduleDurationContainer> bonusScheduleDurationContainers
= scheduleDurationContainersOfWeek.stream()
.filter(s -> s.getContainerType() == ScheduleIntervalContainerTypeEnum.BONUS)
.collect(Collectors.toList());
final ScheduleDurationContainer bonusScheduleDurationContainer
= bonusScheduleDurationContainers.stream()
.filter(s -> s.getDayOfWeekStartingWithZero() == dayOfWeekTmp)
.findFirst()
.get();