我该如何使用流将此for循环转换?
for (final Foo foo : fooList) {
if (!FooConstant.FOO_TYPE.equals(foo.getType())) {
throw new BadRequestException(
LOGGER,
FooErrorEnum.NOT_FOO_TYPE.name(),
String.format("Element %s is not Foo type", foo.getId())
);
}
}
我尝试这样做,但在 orElseThrow()
中没有可用的 foo.getId()
。
注意:BadRequestException 是已检查异常。
fooList.stream().filter(foo -> !FooConstant.FOO_TYPE.equals(foo.getType())
.findFirst()
.orElseThrow(() -> new BadRequestException(LOGGER, FooErrorEnum.NOT_FOO_TYPE.name(), String.format("Element %s is not Foo type", foo.getId()));
ifPresent
或orElseThrow
中抛出已检查异常。请阅读以下内容:https://dev59.com/KV4c5IYBdhLWcg3wscDI - Andrew Tobilko