Skip to content

Commit ac95e09

Browse files
committed
docs(streams): add detailed guide and examples for Java 8 Streams API
What - Added comprehensive documentation explaining **Java 8 Streams**: - Definition of a Stream (sequence of elements from collections/arrays/I/O). - Key characteristics: * Functional style (lambdas & method references). * Lazy evaluation (intermediates executed only when terminal ops run). * Pipeline processing (Source → Intermediate → Terminal). * No side effects (encourages pure, stateless functions). * Parallel execution (parallelStream() support). - Classification of operations: * Intermediate (filter, map, sorted, etc.) → lazy. * Terminal (forEach, collect, count, reduce, etc.) → eager, consume the stream. - Stream workflow steps: creation, transformation, terminal evaluation. - Added **example programs**: 1. **Basic Filtering** - Filters names starting with `"A"` and prints them. - Demonstrates `.filter(...)` + `.forEach(...)`. ```java names.stream().filter(n -> n.startsWith("A")).forEach(System.out::println); ``` ✅ Output: `Alice`. 2. **Collecting Results** - Filters even numbers into a new `List<Integer>`. - Uses `.collect(Collectors.toList())`. ✅ Output: `[2, 4]`. 3. **Mapping and Reducing** - Sums integers using `.reduce(0, Integer::sum)`. ✅ Output: `Sum: 15`. 4. **Parallel Streams** - Uses `.parallelStream()` to process elements in parallel. - Demonstrates unordered parallel execution. Why - Streams represent a **functional and declarative approach** to data processing in Java 8. - They simplify common tasks like filtering, mapping, reducing, and collecting into **concise, readable pipelines**. - Understanding Streams is essential for: - Backend data manipulation (Spring Boot, microservices). - Parallel processing of large datasets. - Replacing verbose imperative loops with functional style. Logic 1. **Streams don’t store data** → they work over a data source. 2. **Intermediate ops are lazy** → nothing executes until a terminal operation is invoked. 3. **Terminal ops trigger execution** and consume the stream. 4. **Parallel streams** split workload across CPU cores for performance on large independent tasks. 5. **Pipeline processing** allows composition of multiple operations in a single fluent chain. Key Takeaways ✔ Streams = functional, lazy, declarative. ✔ Intermediate ops = transformations (filter/map/sorted). ✔ Terminal ops = results (collect/reduce/forEach). ✔ Parallel streams = enable concurrency with minimal effort. ✔ Benefits = cleaner code, reduced boilerplate, efficient execution. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 108e4b3 commit ac95e09

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
In Java 8, Streams are a powerful and versatile API that allows you to process collections of data (like List, Set, and Map) in a functional and declarative way. They enable you to perform complex data transformations with minimal code by chaining operations together.
1+
In Java 8, Streams are a powerful and versatile API that allows you to process collections of
2+
data (like List, Set, and Map) in a functional and declarative way.
23

3-
✅ What is a Stream?
4+
They enable you to perform complex data transformations with minimal code by chaining operations together.
45

5-
A Stream is a sequence of elements that you can process in a pipeline of operations. It does not store data; instead, it works on the data from a source (like a collection, array, or I/O channel).
6+
What is a Stream?
7+
A Stream is a sequence of elements that you can process in a pipeline of operations.
8+
It does not store data; instead, it works on the data from a source (like a collection, array, or I/O channel).
69

7-
Key Characteristics of Streams:
10+
Key Characteristics of Streams:
811

912
Functional Style – Emphasizes the use of lambda expressions and method references for concise and readable code.
1013
Lazy Evaluation – Operations are executed only when a terminal operation is called.
1114
Pipeline Processing – Streams support a pipeline of operations: Source → Intermediate Operations → Terminal Operation.
1215
No Side Effects – Streams encourage pure functions that do not modify the original data.
1316
Parallel Execution – Can process data in parallel using the parallelStream() method.
1417

15-
Stream Operations:
18+
Stream Operations:
1619

17-
Intermediate Operations – Transform the stream (lazy evaluation). Examples:
20+
Intermediate Operations – Transform the stream (lazy evaluation).
21+
22+
Examples:
1823

1924
filter() – Filters elements based on a condition.
2025
map() – Transforms each element.
@@ -25,13 +30,13 @@ forEach() – Iterates over each element.
2530
collect() – Collects results into a collection.
2631
count() – Counts the elements.
2732

28-
Stream Workflow:
33+
Stream Workflow:
2934

3035
Create a Stream – From collections, arrays, or other data sources.
3136
Apply Intermediate Operations – Transform or filter the stream.
3237
Perform Terminal Operation – Generate output or apply side effects.
3338

34-
Example 1: Basic Stream Operations
39+
Example 1: Basic Stream Operations
3540

3641
import java.util.*;
3742
import java.util.stream.*;
@@ -48,10 +53,9 @@ public class Java8Demo {
4853
}
4954

5055
Output:
51-
5256
Alice
5357

54-
Example 2: Collecting Results
58+
Example 2: Collecting Results
5559

5660
import java.util.*;
5761
import java.util.stream.*;
@@ -70,10 +74,9 @@ public class Java8Demo {
7074
}
7175

7276
Output:
73-
7477
[2, 4]
7578

76-
Example 3: Mapping and Reducing
79+
Example 3: Mapping and Reducing
7780

7881
import java.util.*;
7982

@@ -90,14 +93,11 @@ public class Java8Demo {
9093
}
9194

9295
Output:
93-
9496
Sum: 15
9597

96-
✅ Parallel Streams
97-
98+
Parallel Streams:
9899
For large datasets, you can use parallelStream() to process data in parallel:
99100

100-
101101
import java.util.*;
102102

103103
public class Java8Demo {
@@ -110,8 +110,7 @@ public class Java8Demo {
110110
}
111111
}
112112

113-
✅ Advantages of Streams:
114-
113+
Advantages of Streams:
115114
Simplifies Code – Reduces boilerplate code using functional style.
116115
Efficient Processing – Supports lazy evaluation and parallel execution.
117-
Better Readability – Clear and expressive pipeline for data transformations.
116+
Better Readability – Clear and expressive pipeline for data transformations.

0 commit comments

Comments
 (0)