Quick revision
The short answer
filter and map are stateless intermediate operations; sorted, distinct, limit and skip are stateful intermediate operations, and limit is also short-circuiting, which lets infinite streams finish. forEach, collect and reduce are terminal and not short-circuiting, because they must see every element. anyMatch and findFirst are short-circuiting terminal operations, as are allMatch, noneMatch and findAny.
What to remember
In this answer, we classified stream operations along three lines. filter and map are stateless intermediate operations. sorted, distinct, limit and skip are stateful intermediate operations, and limit is also short-circuiting, so it lets infinite streams finish while skip does not. forEach, collect and reduce are terminal operations that need every element, and anyMatch, allMatch, noneMatch, findFirst and findAny are terminal operations that can stop early.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
filter and map are stateless intermediate operations; sorted, distinct, limit and skip are stateful intermediate operations, and limit is also short-circuiting, which lets infinite streams finish. forEach, collect and reduce are terminal and not short-circuiting, because they must see every element. anyMatch and findFirst are short-circuiting terminal operations, as are allMatch, noneMatch and findAny.
Why this question is asked
Knowing which category an operation belongs to is not trivia. It tells us whether a pipeline can run on an infinite stream, whether it buffers everything in memory, whether it parallelises well and whether a side effect will run for every element. Interviewers ask this classification question to check that we can predict how a pipeline behaves without running it. In this answer, we will define the three distinctions, go through each group of operations with an example, explain why limit and skip count as stateful, and end with one table that classifies all of them.
1A Quick Recap of the Three Distinctions
Intermediate operations return a new stream and are lazy: they only add a stage to the pipeline. Terminal operations produce a result or a side effect and start the actual processing; after a terminal operation the stream is consumed.
Stateless operations handle each element on its own, without remembering anything about other elements. Stateful operations need information about elements they have already seen, such as all previous values for distinct or a count for limit.
Short-circuiting operations can finish without processing all elements. For an intermediate operation, that means it can turn an infinite stream into a finite one. For a terminal operation, it means it can produce a result after seeing only part of the stream.
2Stateless Intermediate: filter and map
filter decides about one element by looking only at that element, and map transforms one element into another. Neither needs memory of earlier elements, so they can pass each element on immediately, and in parallel each thread can process its chunk without coordination. That is why long chains of filter and map are cheap: they fuse into a single pass.
3Stateful Intermediate: sorted, distinct, limit and skip
sorted must see every element before it can emit the first one, so it buffers the whole stream. distinct must remember every element it has passed so far, in a set, to drop later duplicates. limit(n) must count how many elements it has passed, and skip(n) must count how many it has dropped. Counting is state too, and on an ordered parallel stream it is expensive state, because "the first n" depends on encounter order across all threads.
limit is special because it is also short-circuiting: once n elements have passed, it tells the pipeline that no more are needed.
Notice the difference between the two. Both are stateful, but only limit can end the stream. That is why an infinite source must always reach a short-circuiting operation, either limit, takeWhile or a short-circuiting terminal.
4Terminal, Not Short-Circuiting: forEach, collect and reduce
forEach performs an action on every element, collect accumulates every element into a container such as a list or map, and reduce combines every element into a single value. By their nature they need all elements, so they never stop early and they never finish on an infinite stream.
5Terminal, Short-Circuiting: anyMatch, findFirst and Friends
anyMatch returns true as soon as one element matches, allMatch returns false as soon as one element does not match, noneMatch returns false as soon as one matches, and findFirst and findAny return as soon as they have one element. Each can stop the pipeline early.
Notice that the peek output proves the short-circuit: the elements after 50 are never pulled from the source. The same pipeline with an infinite source would still finish as soon as a match appears, although it would run forever if no element ever matched.
6All Operations in One Table
Putting the categories together gives a quick way to read any pipeline: look for stateful stages to see where memory and parallel cost go, and look for short-circuiting stages to see whether the pipeline can stop early.
| Operation | Kind | State | Short-circuiting |
|---|---|---|---|
| filter, map | intermediate | stateless | no |
| sorted | intermediate | stateful, buffers all | no |
| distinct | intermediate | stateful, remembers seen | no |
| limit | intermediate | stateful, counts | yes |
| skip | intermediate | stateful, counts | no |
forEach, collect, reduce | terminal | - | no |
anyMatch, allMatch, noneMatch | terminal | - | yes |
findFirst, findAny | terminal | - | yes |
7Follow-up questions
Is takeWhile stateful or stateless?
takeWhile, added in Java 9, is short-circuiting: once the predicate fails, it stops the stream. On a sequential stream it needs almost no state, but on an ordered parallel stream it must respect encounter order, which makes it expensive in the same way as limit. The documentation treats it like the other order-dependent operations, so check the details for parallel use.
Why is limit expensive on an ordered parallel stream?
Because the first n elements in encounter order may be spread across many chunks handled by different threads. The framework has to buffer and coordinate to make sure it returns exactly the first n. If order does not matter, calling unordered() first lets limit take any n elements, which is much cheaper.
Is count() short-circuiting?
No, it is a non-short-circuiting terminal operation. However, since Java 9 it may not traverse the pipeline at all when the size is known from the source, which is a different optimisation from short-circuiting.
8Conclusion
In this answer, we classified stream operations along three lines. filter and map are stateless intermediate operations. sorted, distinct, limit and skip are stateful intermediate operations, and limit is also short-circuiting, so it lets infinite streams finish while skip does not. forEach, collect and reduce are terminal operations that need every element, and anyMatch, allMatch, noneMatch, findFirst and findAny are terminal operations that can stop early.
Open with
I think about three questions for each operation: does it return a stream or a result, does it need memory of other elements, and can it stop early. filter and map are stateless intermediate operations.
Then, if they want more
Each element is handled on its own and passed on immediately, which is why chains of them fuse into one cheap pass and parallelise easily. sorted, distinct, limit and skip are stateful intermediate operations. sorted has to buffer everything before emitting the first element, distinct keeps a set of what it has seen, and limit and skip have to count, which on an ordered parallel stream means coordinating encounter order across threads. limit is also short-circuiting: once it has n elements it stops the pipeline, so Stream iterate with limit five finishes, whereas skip on an infinite stream never does.
For terminals, forEach, collect and reduce are not short-circuiting, they must see every element. anyMatch and findFirst are short-circuiting terminals, and so are allMatch, noneMatch and findAny: anyMatch returns true at the first match, and with a peek you can see the later elements are never pulled.
Knowing this tells me where a pipeline holds memory, whether it can run on an infinite source and whether side effects run for every element.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- 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.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
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 "Classify these operations as intermediate or terminal, stateless or stateful, and...": https://scrivio-interview-library.netlify.app/java/streams/3676948a09/