在lambda中抛出异常 - java 8

Sue*_*Sue 2 java lambda java-8

我正在使用lambda表达式.相当于这个:

for (Integer id: ids) {
    if (!repository.exists(id)) {
        throw new Exception .....
    }
}
Run Code Online (Sandbox Code Playgroud)

我试过用这个:

ids.stream().filter(id-> repository.exists(idStatut)).findAny().orElseThrow(() ->
                new Exception...
            );
Run Code Online (Sandbox Code Playgroud)

但它效果不好

Era*_*ran 5

基于原始循环,如果任何Integers未通过过滤器,则需要抛出异常:

if (ids.stream().anyMatch(id -> !repository.exists(id)))
    throw new Exception ...
Run Code Online (Sandbox Code Playgroud)