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 OverflowBaeldung
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.
Baeldung
baeldung.com › home › java › java streams › how to iterate over a stream with indices
How to Iterate Over a Stream With Indices | Baeldung
February 28, 2025 - Learn several ways of iterating over Java 8 Streams using indices
Videos
16:19
Java Stream API with iterate() method || Java8 Stream interface ...
00:57
Java Streams iterate() in 1 min | #geekific #iterate #in1min - YouTube
14:52
Java 8 forEach Method Tutorial | Iterate over List, Set, Stream ...
08:50
Java Streams Crash Course: Everything You Need to Know - YouTube
00:50
Stream.iterate vs. for-loop #java #shorts #coding #airhacks - YouTube
09:29
Java Stream API forEach() Filter - YouTube
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));
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.
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 ...
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.