2

I'm using lambda expressions. What is the equivalent of this:

for (Integer id: ids) {
    if (!repository.exists(id)) {
        throw new Exception .....
    }
}

I tried using this:

ids.stream().filter(id-> repository.exists(idStatut)).findAny().orElseThrow(() ->
                new Exception...
            );

But it doesn't work well

1 Answer 1

5

Based on your original loop, you want to throw an exception if any of the Integers don't pass the filter:

if (ids.stream().anyMatch(id -> !repository.exists(id)))
    throw new Exception ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! I wasted all morning on it -_-"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.