Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What does it mean that streams are lazy? What does this print?

Java
Stream.of("a", "b", "c").filter(s -> {
System.out.println("filter " + s);
return true;
}).map(String::toUpperCase).findFirst();

Streams are lazy because intermediate operations such as filter and map only describe the pipeline; nothing runs until a terminal operation such as findFirst asks for a result. Then elements flow one at a time through all stages, and findFirst is short-circuiting, so processing stops as soon as one element comes out. The code prints only filter a. Without the terminal operation it would print nothing at all.

Why this question is asked

Laziness is the property of streams that explains most surprises: a map that never runs, a peek that prints in a strange order, a side effect that happens only for some elements, or an infinite stream that finishes. Interviewers often show a small snippet like this one and ask what it prints, because the answer reveals whether we picture a stream as a series of loops, one per stage, or as a single pass. In this answer, we will recap intermediate and terminal operations, trace the snippet element by element, change it slightly to see the order more clearly, look at what short-circuiting does, and see what happens without the terminal operation.

1A Quick Recap of Intermediate and Terminal Operations

A stream pipeline has three parts: a source, such as a collection or Stream.of(...), zero or more intermediate operations, such as filter, map and sorted, which each return a new stream, and exactly one terminal operation, such as findFirst, forEach, collect or count, which produces a result or a side effect.

Intermediate operations are lazy. Calling filter does not filter anything; it adds a stage to a description of the pipeline and returns immediately. Only the terminal operation starts the work, and it pulls elements from the source through all the stages.

2Tracing the Snippet Element by Element

Here is the code from the question with what happens at each step.

Java
Stream.of("a", "b", "c")
.filter(s -> { System.out.println("filter " + s); return true; }) // nothing yet
.map(String::toUpperCase) // nothing yet
.findFirst(); // starts the work
// Output:
// filter a
Step throughThe single pass that findFirst drives
SourcefiltermapfindFirst
"a"
prints filter a, returns true
"a"
"A"
done: stop sending elements

5 messages in order. Play them, or step through them one at a time.

"b" and "c" are never pulled from the source.

When findFirst runs, the stream takes the first element, "a", and passes it to filter, which prints filter a and returns true. Because it passed, "a" goes straight on to map, which turns it into "A", and then to findFirst, which now has its answer, Optional[A]. findFirst tells the pipeline it needs nothing more, so "b" and "c" are never taken from the source, and filter never sees them. That is why only one line is printed.

3One Element at a Time, Not One Stage at a Time

A common wrong picture is that filter runs over all three elements first, then map runs over the results, then findFirst picks one. That would print filter a, filter b and filter c. We can see the real order more clearly by making filter reject "a" and printing in map too.

Java
Stream.of("a", "b", "c")
.filter(s -> { System.out.println("filter " + s); return !s.equals("a"); })
.map(s -> { System.out.println("map " + s); return s.toUpperCase(); })
.findFirst();
// filter a "a" is rejected, so map never sees it
// filter b "b" passes
// map b and goes straight to map
// findFirst has B, so "c" is never touched

Notice that map b comes right after filter b, before anything happens to "c". Each element travels through the whole chain before the next one starts. This design is called vertical or element-at-a-time processing, and it is the reason a stream with ten stages still makes only one pass over the data and needs no temporary lists between stages.

4What Short-Circuiting Adds

findFirst is a short-circuiting terminal operation: it can finish without seeing every element. Others are anyMatch, allMatch, noneMatch and findAny, and among intermediate operations, limit. Laziness and short-circuiting together are what make infinite streams usable.

Java
Optional<Integer> first = Stream.iterate(1, n -> n + 1) // infinite: 1, 2, 3, ...
.filter(n -> n % 7 == 0)
.findFirst();
System.out.println(first.get()); // 7, after looking at only 7 elements

If streams processed stage by stage, filter would try to finish the infinite source before passing anything on, and the program would never end. Because elements flow one at a time and findFirst stops the flow, it finishes after seven elements.

5Without the Terminal Operation, Nothing Happens

Remove findFirst and the same code prints nothing, because no stage ever runs.

Java
Stream<String> s = Stream.of("a", "b", "c")
.filter(x -> { System.out.println("filter " + x); return true; })
.map(String::toUpperCase);
// no output: the pipeline is only described
s.forEach(x -> {}); // now it prints filter a, filter b, filter c

This is the source of a common bug, where someone writes list.stream().map(this::save) as a statement and wonders why nothing is saved. A stream without a terminal operation is just a plan.

6Follow-up questions

Does sorted() change the order of the printed lines?

Yes. sorted is a stateful operation that must see every element before it can emit the smallest one, so it acts as a barrier: all elements pass through the stages before sorted, then sorted releases them one by one to the stages after it. A filter placed before sorted would print for every element, even with findFirst at the end.

Is findFirst on a parallel stream still guaranteed to return "A"?

Yes, findFirst respects encounter order, so it returns the first element in source order even in parallel, though more elements may be processed along the way. findAny is allowed to return any matching element and can be faster in parallel.

Why is laziness useful beyond infinite streams?

It lets the library fuse all stages into one pass, avoid building intermediate collections, and skip work that cannot affect the result, as findFirst and limit do. For large data sets that means less memory and less time.

7Conclusion

In this answer, we saw that stream intermediate operations only build a pipeline and that the terminal operation drives all the work. Elements flow one at a time through every stage, and short-circuiting operations such as findFirst stop the flow as soon as they have their answer. So the snippet prints only filter a, a variant with a rejected element prints filter a, filter b, map b, and the same pipeline without a terminal operation prints nothing at all.

What to say in the interviewAbout 80 seconds aloud

Open with

Streams are lazy in the sense that intermediate operations like filter and map don't do any work when you call them. They just add a stage to the pipeline and return a new stream.

Then, if they want more

The work starts only when a terminal operation, here findFirst, asks for a result. Then elements are pulled from the source one at a time, and each element goes through all the stages before the next one starts, rather than filter running over everything first and then map.

So in this code, findFirst pulls a, filter prints filter a and returns true, map turns it into capital A, and findFirst has its answer. findFirst is short-circuiting, so it tells the pipeline to stop, and b and c are never taken from the source. The output is just one line: filter a.

If we removed findFirst, nothing would be printed at all, because without a terminal operation the pipeline is only a description.

This combination, element-at-a-time processing plus short-circuiting, is also why something like Stream iterate with a filter and findFirst can finish on an infinite stream, and it's why stateful stages like sorted behave differently: they have to buffer all elements before passing anything on.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does it mean that streams are lazy? What does this print?": https://scrivio-interview-library.netlify.app/java/streams/19608f61f3/

More in Streams API