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?
Stream#anyMatch? Or how does that not work for you?forEachsimply does what it says: It executes an action for each element, not less.