Streams Are Not Data Structures
What You'll Learn
You'll understand that streams don't store elements themselves—they're pipelines that convey elements from a source through operations.
The Core Idea
When you first encounter Stream<T>, it's tempting to think of it as another collection like ArrayList or HashSet. This is incorrect. A stream does not hold or store data. Instead, it's a channel that pulls elements from a source (like a collection, array, or generator) and pushes them through a series of computational operations.
Think of it this way: a stream is like a conveyor belt in a factory. The belt itself doesn't store the items—it transports them from one station to another where work gets done. The actual items are stored in the warehouse (your collection or data source).
Why This Matters
Since streams don't store elements:
- No indexing: You can't ask for "the 5th element" of a stream like you can with a
List. - Single-use only: Once you've processed a stream, it's consumed and cannot be reused.
- No modification: Streams don't have
add()orremove()methods—they transform data without changing the original source. - Lazy evaluation: Operations can be optimized because the stream doesn't need to materialize all results immediately.
Your data lives in structures like ArrayList, HashSet, or arrays. Streams are the processing pipelines you build on top of that data to filter, map, sort, or aggregate it declaratively.
Key Takeaway: Streams are ephemeral pipelines for processing data, not containers for storing it—they convey elements from a source through operations without holding the data themselves.