Using return; will work just fine. It will not prevent the full loop from completing. It will only stop executing the current iteration of the forEach loop.

Try the following little program:

public static void main(String[] args) {
    ArrayList<String> stringList = new ArrayList<>();
    stringList.add("a");
    stringList.add("b");
    stringList.add("c");

    stringList.stream().forEach(str -> {
        if (str.equals("b")) return; // only skips this iteration.

        System.out.println(str);
    });
}

Output:

a
c

Notice how the return; is executed for the b iteration, but c prints on the following iteration just fine.

Why does this work?

The reason the behavior seems unintuitive at first is because we are used to the return statement interrupting the execution of the whole method. So in this case, we expect the main method execution as a whole to be halted.

However, what needs to be understood is that a lambda expression, such as:

str -> {
    if (str.equals("b")) return;

    System.out.println(str);
}

... really needs to be considered as its own distinct "method", completely separate from the main method, despite it being conveniently located within it. So really, the return statement only halts the execution of the lambda expression.

The second thing that needs to be understood is that:

stringList.stream().forEach()

... is really just a normal loop under the covers that executes the lambda expression for every iteration.

With these 2 points in mind, the above code can be rewritten in the following equivalent way (for educational purposes only):

public static void main(String[] args) {
    ArrayList<String> stringList = new ArrayList<>();
    stringList.add("a");
    stringList.add("b");
    stringList.add("c");

    for(String s : stringList) {
        lambdaExpressionEquivalent(s);
    }
}

private static void lambdaExpressionEquivalent(String str) {
    if (str.equals("b")) {
        return;
    }

    System.out.println(str);
}

With this "less magic" code equivalent, the scope of the return statement becomes more apparent.

Answer from sstan on Stack Overflow
Top answer
1 of 5
480

Using return; will work just fine. It will not prevent the full loop from completing. It will only stop executing the current iteration of the forEach loop.

Try the following little program:

public static void main(String[] args) {
    ArrayList<String> stringList = new ArrayList<>();
    stringList.add("a");
    stringList.add("b");
    stringList.add("c");

    stringList.stream().forEach(str -> {
        if (str.equals("b")) return; // only skips this iteration.

        System.out.println(str);
    });
}

Output:

a
c

Notice how the return; is executed for the b iteration, but c prints on the following iteration just fine.

Why does this work?

The reason the behavior seems unintuitive at first is because we are used to the return statement interrupting the execution of the whole method. So in this case, we expect the main method execution as a whole to be halted.

However, what needs to be understood is that a lambda expression, such as:

str -> {
    if (str.equals("b")) return;

    System.out.println(str);
}

... really needs to be considered as its own distinct "method", completely separate from the main method, despite it being conveniently located within it. So really, the return statement only halts the execution of the lambda expression.

The second thing that needs to be understood is that:

stringList.stream().forEach()

... is really just a normal loop under the covers that executes the lambda expression for every iteration.

With these 2 points in mind, the above code can be rewritten in the following equivalent way (for educational purposes only):

public static void main(String[] args) {
    ArrayList<String> stringList = new ArrayList<>();
    stringList.add("a");
    stringList.add("b");
    stringList.add("c");

    for(String s : stringList) {
        lambdaExpressionEquivalent(s);
    }
}

private static void lambdaExpressionEquivalent(String str) {
    if (str.equals("b")) {
        return;
    }

    System.out.println(str);
}

With this "less magic" code equivalent, the scope of the return statement becomes more apparent.

2 of 5
9

Another solution: go through a filter with your inverted conditions : Example :

if(subscribtion.isOnce() && subscribtion.isCalled()){
                continue;
}

can be replaced with

.filter(s -> !(s.isOnce() && s.isCalled()))

The most straightforward approach seem to be using "return;" though.

🌐
TutorialsPoint
tutorialspoint.com › break-or-return-from-java-8-stream-foreach
Break or return from Java 8 stream forEach?
But what if we actually wanted to stop processing whenever we saw "Bob"? The forEach does not use traditional loop so it does not allow to use break or continue statement in lambda expression inside of it.
Discussions

java - How to put continue in side forEach loop in java8 - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Closed 9 years ago. How to write continue statement inside forEach loop in java 8. More on stackoverflow.com
🌐 stackoverflow.com
Advice on defending the use of continue and break in a foreach loop in Java
He is wrong. Multiple return points are a good thing. Returning once done makes the code more concise. Show him Martin Fowler's discussion of break, continue and multiple returns in the refactoring "Remove Control Flag" in his book Refactoring Improving The Design Of Existing Code. More on reddit.com
🌐 r/java
20
7
July 19, 2011
Break or return from Java 8 stream forEach? - Stack Overflow
However I can imagine some useful ... of forEach() or so, for which using exception is not bad practice. I have added a paragraph to my answer to be clear. 2016-06-02T22:07:32.297Z+00:00 ... I think this is a fine solution. After searching Google for "java exceptions" and other searches with a few more words like "best practices" or "unchecked", etc., I see there is controversy over how to use exceptions. I used this solution in my code because the stream was performing ... More on stackoverflow.com
🌐 stackoverflow.com
How to do it in Java 8 stream api foreach continue or break - Stack Overflow
I have following code where I want to break or continue in the for each loop:- List (); Map rowMap2 = new More on stackoverflow.com
🌐 stackoverflow.com
🌐
Techblogss
techblogss.com › java › java_cj_foreach_continue
Java forEach continue break
continue inside loop is not supported by forEach. As a workaround you can return from the loop. // shows how to continue in a forEach loop import java.util.ArrayList; import java.util.List; public class TestClass { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); ...
🌐
Baeldung
baeldung.com › home › java › java streams › how to break from java stream foreach
How to Break from Java Stream forEach | Baeldung
January 25, 2024 - As we can see, the stream stopped after the condition was met. For testing purposes, we’ve collected the results into a list, but we could also have used a forEach call or any of the other functions of Stream.
🌐
Baeldung
baeldung.com › home › java › java collections › the difference between collection.stream().foreach() and collection.foreach()
The Difference Between stream().forEach() and forEach() | Baeldung
September 17, 2025 - Similarly, we’ll get a ConcurrentModification exception when we add or remove an element during the execution of the stream pipeline. However, the exception will be thrown later. Another subtle difference between the two forEach() methods is that Java explicitly allows modifying elements ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 51654187 › how-to-do-it-in-java-8-stream-api-foreach-continue-or-break
How to do it in Java 8 stream api foreach continue or break - Stack Overflow
If all you are going to do, is forEach, you are not using any advantage of the Stream API. You could just use dataSource.forEach(…) here, but even that has no advantage over a for loop, given that huge loop body. Besides that, you could get the equivalent of continue by simply using return within the lambda expression, but there is no equivalent to break here.
🌐
W3Docs
w3docs.com › java
Break or return from Java 8 stream forEach?
To break or return from a Java 8 Stream.forEach() operation, you can use the break or return statements as you would normally do in a loop. However, since forEach() is a terminal operation and is not intended to be used as a loop, it is generally better to use a different method such as findFirst() or anyMatch() if you only need to find a single element in the stream.
🌐
Javaprogramto
javaprogramto.com › 2020 › 05 › java-break-return-stream-foreach.html
How to Break or return from Java Stream forEach in Java 8 JavaProgramTo.com
May 13, 2020 - If the stream is an extremely long one then you need to traverse till last value to end the loop. Our requirement is to stop the loop once out condition is met then forEach does not much help on this.
🌐
Codemia
codemia.io › knowledge-hub › path › break_or_return_from_java_8_stream_foreach
Break or return from Java 8 stream forEach?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Java67
java67.com › 2016 › 01 › how-to-use-foreach-method-in-java-8-examples.html
10 Examples of forEach() method in Java 8 | Java67
You can read more about that in the Collections to Streams in Java 8 Using the Lambda Expressions course on Pluralsight, which provides an in-depth explanation of new Java 8 features. So far you have both basic and advanced examples of using the forEach() method, first with simply iterating over each element and then along with using the filter() method, Let's see one more example of the forEach() method along with the map() function, which is another key functionality of Stream API.
🌐
GeeksforGeeks
geeksforgeeks.org › java › foreach-loop-vs-stream-foreach-vs-parallel-stream-foreach
foreach() loop vs Stream foreach() vs Parallel Stream foreach() - GeeksforGeeks
July 12, 2025 - Lambda operator is used: In stream().forEach(), lambdas are used and thus operations on variables outside the loop are not allowed. Only the operations on concerned collections are possible. In this, we can perform operations on Collections by single line code that makes it easy and simple to code. ... import Java.util.*; public class GFG { public static void main(String[] args) throws Exception { List<String> arr1 = new ArrayList<String>(); int count = 0; arr1.add("Geeks"); arr1.add("For"); arr1.add("Geeks"); arr1.stream().forEach(s -> { // this will cause an error count++; // print all elements System.out.print(s); }); } }
🌐
Baeldung
baeldung.com › home › java › core java › guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - The article is an example-heavy introduction of the possibilities and operations offered by the Java 8 Stream API. ... In Java, the Collection interface has Iterable as its super interface. This interface has a new API starting with Java 8: ... Simply put, the Javadoc of forEach states that it “performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.”
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream foreach()
Java Stream forEach() with Examples - HowToDoInJava
March 14, 2022 - Consumer is a functional interface and action represents a non-interfering action to be performed on each element in the Stream. It accepts an input and returns no result. The forEach() method is a terminal operation. It means that it does not return an output of type Stream.
🌐
Tutorialspoint
tutorialspoint.com › java › java_continue_statement.htm
Java - continue Statement
Java Vs. C++ ... The continue statement can be used in any loop control structure to skip the current iteration and jump to the next one. In a for loop, it immediately transfers control to the update statement, while in a while or do-while loop, it jumps directly to the Boolean expression for the next iteration. The syntax of a continue is a single statement inside any loop − ... See the following image to know how continue statement works to skip the current iteration of the loop:
🌐
Reddit
reddit.com › r/java › 3 reasons why you shouldn’t replace your for-loops by stream.foreach()
r/java on Reddit: 3 Reasons why You Shouldn’t Replace Your for-loops by Stream.forEach()
December 8, 2015 - News, Technical discussions, research papers and assorted things of interest related to the Java programming language NO programming help, NO learning Java related questions, NO installing or downloading Java questions, NO JVM languages - Exclusively Java ... 繁體中文Português (Brasil)日本語FrançaisNorsk (Bokmål)FilipinoEspañolΕλληνικάSvenskaRomânăSuomiHindi (Latin)Español (Latinoamérica) ... Archived post. New comments cannot be posted and votes cannot be cast. Share ... The purpose of Streams is not as some sort of replacement to for loops. If you just have a list you need to iterate over, it's obvious that Stream.forEach isn't necessary.