Basically I have a list and its elements will be processed one by one until some condition is met. If that condition is met for any element, it should return true
otherwise false
. The method looks like:
public boolean method(List<Integer> data) {
data.forEach(item -> {
if (some condition is met) {
return true; // Getting Unexpected return value here
}
});
return false;
}
Is there a way to break out of this forEach loop right after the condition is met instead of looping through all the elements?
data.stream().anyMatch(item -> {condition})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With