☕️ What are Java Streams?
✅ Java Streams are part of the java.util.stream
package.
✅ They provide a functional-style way to process sequences of elements (like collections, arrays, files, etc.) in a declarative manner.
🔗 Key Points
What it is | Explanation |
---|---|
Stream | A sequence of data elements (from a collection, array, or I/O source) that can be processed in a pipeline. |
Pipeline | A series of steps where data flows through transformations (filters, maps, etc.) and ends with a result (a collection, sum, or side effect). |
Functional | Encourages writing what to do, not how to do it (declarative style). |
One-time use | Streams cannot be reused — once consumed, they are gone. |
📦 How to Create a Stream
Example – from a List
List<String> names = List.of("Alice", "Bob", "Charlie");
Stream<String> stream = names.stream();
🛠️ Common Stream Operations
Type | Examples | Explanation |
---|---|---|
Creation | stream() , Stream.of() | Create a stream from data sources |
Intermediate | filter() , map() , sorted() | Transform the stream (lazy) |
Terminal | collect() , forEach() , count() | Final result (triggers processing) |
🌊 Example – Complete Stream Pipeline
List<String> names = List.of("Alice", "Bob", "Charlie");
List<String> filteredNames = names.stream()
.filter(name -> name.startsWith("A")) // Intermediate
.map(String::toUpperCase) // Intermediate
.sorted() // Intermediate
.collect(Collectors.toList()); // Terminal
System.out.println(filteredNames); // [ALICE]
✅ Processing is lazy — nothing happens until a terminal operation (collect()
) is called.
✅ This is a pipeline: Source ➡️ Filter ➡️ Map ➡️ Sort ➡️ Collect.
💨 Streams vs Collections
Aspect | Collection | Stream |
---|---|---|
Storage | Stores data | Does not store data |
Reuse | Can be iterated many times | Can be consumed only once |
Eager/Lazy | Processing is eager (immediate) | Processing is lazy (only happens when terminal operation is called) |
External/Internal iteration | You iterate manually (for loop) | Stream handles iteration internally |
⚙️ Parallel Streams
Streams can also run in parallel to leverage multi-core processors.
List<String> names = List.of("Alice", "Bob", "Charlie");
long count = names.parallelStream()
.filter(name -> name.startsWith("A"))
.count();
✅ Each operation may run in a different thread, improving performance on large datasets.
💥 Common Use Cases
Use Case | Example |
---|---|
Filter data | Names starting with “A” |
Transform data | Convert all names to uppercase |
Aggregation | Sum of all prices |
Collecting result | Convert stream back to List/Set/Map |
🚨 Key Things to Remember
✅ Stream ≠ Collection — it’s a view, not storage.
✅ One-time use — you cannot reuse a stream.
✅ Lazy evaluation — intermediate steps only run if a terminal operation is present.
✅ Functional-style — write what to do, not how to do it.
🚀 Quick Recap
Step | Example |
---|---|
Create Stream | list.stream() |
Intermediate ops | filter() , map() |
Terminal op | collect() , count() |