Question: Have the following codes with breaking behavior in a for loop:

```
package test;

import java.util.Arrays;
import java.util.List;

public class Test {

    private static List<Integer> integerList = Arrays.asList(1, 2, 3, 4);

    public static void main(String[] args) {
        countTo2(integerList);
    }

    public static void countTo2(List<Integer> integerList) {

        for (Integer integer : integerList) {
            System.out.println("counting " + integer);
            if (integer >= 2) {
                System.out.println("returning!");
                return;
            }
        }
    }
}

```

trying to express it with Lambda using `forEach()` will change the behavior as the for loop is not breaking anymore:

```
public static void countTo2(List<Integer> integerList) {

    integerList.forEach(integer -> {
        System.out.println("counting " + integer);
        if (integer >= 2) {
            System.out.println("returning!");
            return;
        }
    });
}

```

This actually makes sense as the `return;` statements are only enforced within the lambda expression itself (within the internal iteration) and not for the whole execution sequence, so is there a way to get the desired behavior (breaking the for loop) using the lambda expression?

Answer: 
What you are looking for is a `short-circuit` terminal operation and while this is the way to do it:

```
integerList.stream()
            .peek(x -> System.out.println("counting = " + x))
            .filter(x -> x >= 2)
            .findFirst()
            .ifPresent(x -> System.out.println("retunrning"));

```

That's an equivalent only when dealing with `sequential` stream. As soon as you add `parallel` that `peek` might show elements that you would not expect, because there is no defined `processing order`, but there is `encounter order` - meaning that elements will be correctly fed to the terminal operation.
One way I could think of doing that would be using `anyMatch` and the inverse:

```
if (integerList.stream().noneMatch(val -> val >= 2)) {
    System.out.println("counting " + val);
}

if (integerList.stream().anyMatch(val -> val >= 2)) {
    System.out.println("returning!");
}

```

but internally that would iterate over the list twice and wouldn't be very optimal I believe.