我正在使用 lambda 表达式。这相当于什么:
for (Integer id: ids) {
if (!repository.exists(id)) {
throw new Exception .....
}
}
我尝试使用这个:
ids.stream().filter(id-> repository.exists(idStatut)).findAny().orElseThrow(() ->
new Exception...
);
但是效果不太好
最佳答案
根据您的原始循环,如果任何 Integer
未通过过滤器,您希望引发异常:
if (ids.stream().anyMatch(id -> !repository.exists(id)))
throw new Exception ...
关于java - 在 lambda 中抛出异常 - java 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47990208/