Terminal vs intermediate operators
🌊 Intermediate Operators (the “lazy transformers”)¶
Think of a Java Stream like a conveyor belt in a smart factory 🏭. Nothing moves until you press the start button… and that button is a terminal operation.
These are operations that prepare or transform the stream, but don’t actually process data yet.
Key traits:¶
- 💤 Lazy → They don’t run immediately
- 🔁 Return another Stream → so you can chain more operations
- 🧱 Build a pipeline, step by step
Common examples:¶
filter()map()sorted()distinct()limit()skip()
Example:¶
👉 Nothing executes yet. The conveyor belt is still idle.
🔚 Terminal Operators (the “execution trigger”)¶
These operations trigger the actual processing of the stream.
Key traits:¶
- ⚡ Eager → Execution starts here
- 🧾 Produce a result or side-effect
- 🚫 Do NOT return a Stream
Common examples:¶
forEach()collect()count()findFirst()anyMatch()reduce()
Example:¶
👉 Now everything runs. Data flows through the pipeline.
⚔️ Quick Comparison¶
| Feature | Intermediate | Terminal |
|---|---|---|
| Execution | Lazy 💤 | Immediate ⚡ |
| Returns | Stream | Result / void |
| Chaining | Yes | Ends pipeline |
| Example | map(), filter() |
collect(), count() |
🧠 One-line memory trick¶
- Intermediate = “What to do”
- Terminal = “Do it now”
⚡ Bonus (important for interviews)¶
Streams use lazy evaluation + pipeline fusion:
👉 Java doesn’t process all elements step-by-step. It processes one element at a time through the whole pipeline, like a VIP pass 🎟️.