我很好奇在以下情况下是否可以使用orElseThrow(),或者是否有更多Java 8的方式将其转换为1行代码?
Collection<Foo> foo = blah.stream().filter(somePredicate).collect(Collectors.toList());
if (foo.isEmpty()) {
throw new Exception("blah");
}
我很好奇在以下情况下是否可以使用orElseThrow(),或者是否有更多Java 8的方式将其转换为1行代码?
Collection<Foo> foo = blah.stream().filter(somePredicate).collect(Collectors.toList());
if (foo.isEmpty()) {
throw new Exception("blah");
}
Collection<Foo> foo = blah.stream().filter(somePredicate)
.collect(Collectors.collectingAndThen(Collectors.toList(), Optional::of))
.filter(l -> !l.isEmpty())
.orElseThrow(() -> new Exception("blah"))
Optional
实例。