Skip to content

Commit 9094b77

Browse files
committed
docs(streams): add beginner-friendly explanation of Java Streams with examples
What - Added a concise overview of **Streams in Java**: - Definition: a way to process collections (List, Set, Map) in a pipeline style. - Workflow: 1. Create → from a collection (`list.stream()`). 2. Process → apply intermediate ops (`filter`, `map`, `sorted`). 3. Output → use terminal ops (`forEach`, `collect`, `count`). - Classified operations into: * Intermediate (lazy) → filter(), map(), sorted(). * Terminal (eager) → forEach(), collect(), count(). - Included a reference table summarizing common operations with their type and purpose. - Added **simple code examples**: 1. **Filter Example** - Filters even numbers from a list. ```java numbers.stream() .filter(n -> n % 2 == 0) .forEach(System.out::println); ``` ✅ Output: `2, 4`. 2. **Map Example** - Squares each number from a list. ```java numbers.stream() .map(n -> n * n) .forEach(System.out::println); ``` ✅ Output: `1, 4, 9`. 3. **Parallel Stream Example** - Demonstrated `.parallelStream()` for concurrent processing of elements in large datasets. Why - This update provides a **beginner-friendly, high-level introduction** to Streams, focusing on clarity and simplicity. - Helps newcomers understand **what a stream is, how the workflow looks, and what common operations exist** before diving into advanced topics. - Examples illustrate functional-style coding, replacing verbose loops with declarative pipelines. Logic 1. **Stream = pipeline abstraction** over a collection. 2. **Intermediate ops** describe *what transformations to apply* but execute lazily. 3. **Terminal ops** trigger the pipeline and produce results. 4. **Parallel streams** leverage multi-core processors automatically for large independent workloads. 5. Provides **cleaner, faster, more maintainable code** compared to imperative loops. Key Takeaways ✔ Stream = sequence of elements processed in a pipeline. ✔ Workflow = Create → Process → Output. ✔ Intermediate = lazy, Terminal = eager. ✔ Common ops = filter, map, sorted, forEach, collect, count. ✔ Parallel streams = automatic concurrency for big datasets. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent ac95e09 commit 9094b77

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
Got it! Here's a simplified overview of Java 8 Streams:
2-
3-
4-
✅ 1. What is a Stream?
5-
1+
1. What is a Stream?
62
A Stream is a way to process collections of data (like List, Set, Map) in a clean and efficient way.
73

84
Think of it as a pipeline where you:
@@ -12,15 +8,14 @@ Think of it as a pipeline where you:
128
3. Get the result (in a new form).
139

1410

15-
2. Basic Stream Workflow:
11+
2. Basic Stream Workflow:
1612

1713
1. Create – From a collection (e.g., list.stream()).
1814
2. Process – Use intermediate operations (like filter() and map()).
1915
3. Output – Use terminal operations (like forEach() or collect()).
2016

2117

22-
✅ 3. Common Stream Operations:
23-
18+
3. Common Stream Operations:
2419

2520
| Operation | Type | Purpose |
2621
|---------------|----------------|-----------------------------|
@@ -32,7 +27,7 @@ Think of it as a pipeline where you:
3227
| count() | Terminal | Count elements. |
3328

3429

35-
4. Simple Examples:
30+
4. Simple Examples:
3631

3732
1. Filter Example: (Get even numbers)
3833

@@ -42,11 +37,9 @@ numbers.stream()
4237
.forEach(System.out::println);
4338

4439
Output:
45-
4640
2
4741
4
4842

49-
5043
2. Map Example: (Square each number)
5144

5245
List<Integer> numbers = Arrays.asList(1, 2, 3);
@@ -55,16 +48,15 @@ numbers.stream()
5548
.forEach(System.out::println);
5649

5750
Output:
58-
5951
1
6052
4
6153
9
6254

63-
5. Parallel Stream (Faster Execution):
55+
5. Parallel Stream (Faster Execution):
6456

6557
Use parallelStream() to process data in parallel for large datasets.
6658

6759
numbers.parallelStream()
6860
.forEach(System.out::println);
6961

70-
💡 Summary: Streams are used to process collections in a clean, fast, and functional way!
62+
Streams are used to process collections in a clean, fast, and functional way.

0 commit comments

Comments
 (0)