【发布时间】:2015-12-30 03:49:18
【问题描述】:
我有以下 lambda 表达式,并且当 bonusScheduleDurationContainers 不为空时可以正常工作。如果它是空的,我会得到一个NoSuchElementException。如何在 lambda 表达式中检查这一点?
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();
【问题讨论】:
标签: lambda java-8 java-stream optional