You need an iterable to be able to use a for-each loop, for example a collection or an array:

for (String s : strings.stream().filter(s->s.length() == 2).toArray(String[]::new)) {

Alternatively, you could completely get rid of the for loop:

strings.stream().filter(s->s.length() == 2).forEach(System.out::println);

You mention you don't want to refactor your for loop but you could extract its body in another method:

strings.stream().filter(s->s.length() == 2).forEach(this::process);

private void process(String s) {
  //body of the for loop
}
Answer from assylias on Stack Overflow
🌐
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 - If we don’t require a stream but only want to iterate over a collection, the first choice should be using forEach() directly on the collection.
🌐
Medium
medium.com › @AlexanderObregon › javas-stream-iterate-method-explained-2922dfd01117
Java’s Stream.iterate() Method Explained | Medium
December 1, 2024 - Learn how Java's Stream.iterate() generates infinite streams, supports lazy evaluation, and handles sequences dynamically with practical examples.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream foreach()
Java Stream forEach() with Examples - HowToDoInJava
March 14, 2022 - Java Stream forEach() method is used to iterate over all the elements of the given Stream and to perform an Consumer action on each element of the Stream.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stream-iteratetpredicateunaryoperator-method-in-java-with-examples
Stream iterate(T,Predicate,UnaryOperator) method in Java with examples - GeeksforGeeks
July 11, 2025 - The iterate(T, java.util.function.Predicate, java.util.function.UnaryOperator) method allows us to iterate stream elements till the specified condition. This method returns a sequential ordered Stream produced by iterative application of the ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › stream › Stream.html
Stream (Java Platform SE 8 )
2 weeks ago - Returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.
🌐
Mkyong
mkyong.com › home › java8 › java 8 stream.iterate examples
Java 8 Stream.iterate examples - Mkyong.com
November 29, 2018 - //Stream.iterate(initial value, next value) Stream.iterate(0, n -> n + 1) .limit(10) .forEach(x -> System.out.println(x));
Find elsewhere
🌐
Medium
neesri.medium.com › about-stream-iterate-e2984e87caea
✍️Stream.iterate in Java 8
July 20, 2025 - Stream.iterate is a method introduced in Java 8 that generates an infinite stream of values based on an initial seed and a function.
🌐
Quora
quora.com › How-do-you-iterate-a-stream-list-in-Java
How to iterate a stream list in Java - Quora
Answer: A stream is not a list. A list is not a stream. You can create one from the other though. A list has the [code ]stream[/code] method to create a stream based on the list. A stream can be collected into a list using the standard [code ]Collectors.toList[/code] method. What you can think ...
🌐
Baeldung
baeldung.com › home › java › java streams › stream to iterable in java
Stream to Iterable in Java | Baeldung
June 27, 2025 - The Java Streams API was introduced in Java 8 and provides functionalities for processing sequences of elements. Streams API supports chaining operations on a Collection of objects in a pipeline in order to produce the desired outcome. In this tutorial, we’ll look into ways of using a Stream as an Iterable.
🌐
TutorialsPoint
tutorialspoint.com › how-to-iterate-list-using-streams-in-java
How to iterate List Using Streams in Java?
June 6, 2025 - We use the stream() method to create a sequential stream from the current list and use forEach() method with lambda expression (->) to iterate over it: import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class iterateList { public static void main(String[] ...
🌐
Java2s
java2s.com › Tutorials › Java_Streams › Tutorial › Streams › Stream_iterate_.htm
Java Streams - Stream.iterate()
import java.util.stream.Stream; /*from www .ja v a 2s. c o m*/ public class Main { public static void main(String[] args) { Stream.iterate(2L, n -> n + 1) .filter(Main::isOdd) .skip(100) .limit(5) .forEach(System.out::println); } public static boolean isOdd(long number) { if (number % 2 == 0) { return false; } return true; } }
🌐
Coderanch
coderanch.com › t › 702090 › java › Beware-parameter-Stream-iterate-method
Beware: three parameter Stream.iterate method might fall one short (Features new in Java 8 forum at Coderanch)
November 11, 2018 - Accordingly it uses an Iterable rather than List as input Another variant, for fun: ... Or: This is a bit troubling though, as it relies on the assumption that the combiner (3rd arg of Collector.of()) will never be called, since we're not using a parallel stream.
🌐
Java Guides
javaguides.net › 2023 › 09 › java-stream-iterate.html
Java Stream iterate()
September 28, 2023 - The iterate() method in the Stream class is a static method used to create a sequential ordered Stream produced by iterative application of a function to an initial element. Each subsequent element is generated by applying the function to the ...
🌐
Stackify
stackify.com › streams-guide-java-8
A Guide to Java Streams: In-Depth Tutorial With Examples
September 4, 2024 - Here, we use short-circuiting operations skip() to skip first three elements, and limit() to limit to five elements from the infinite stream generated using iterate(). We’ll talk more about infinite streams later on. One of the most important characteristics of Java streams is that they allow ...
🌐
nipafx
nipafx.dev › java-9-stream-iterate
Oh No, I Forgot Stream::iterate - nipafx.dev
July 25, 2016 - In Java 9 Stream gets a couple of new methods - one of them is an overload of iterate that takes a predicate and returns a finite stream.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › stream › Stream.html
Stream (Java SE 11 & JDK 11 )
January 20, 2026 - Returns a sequential ordered Stream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.
🌐
Kodejava
kodejava.org › how-do-i-use-stream-iterate-method
How do I use Stream.iterate() method? - Learn Java by Examples
The Stream.iterate() method in Java is used to generate a Stream of elements based on some iterative logic. The simplest form Stream.iterate(initialValue, lambdaFunction) takes in an initial value and a UnaryOperator function to compute the ...