Skip to content

Commit e3c4ef2

Browse files
committed
docs(streams): add explanation and examples for stateful vs stateless operations
What - Introduced a detailed guide distinguishing **stateless** and **stateful** operations in Java Streams. - Covered: 1. **Stateless Operations** - Definition: process each element independently without tracking state. - Examples: filter(), map(), flatMap(), peek(), forEach(). - Added code demo showing filter + map + forEach pipeline. - ✅ Output: 4, 8. 2. **Stateful Operations** - Definition: require tracking previously seen elements or buffering the entire stream. - Examples: distinct(), sorted(), limit(), skip(), collect(), reduce(). - Added code demo showing distinct + sorted pipeline. - ✅ Output: 1, 3, 5, 7. 3. **Performance Considerations** - Stateless = faster, element-by-element, well-suited for parallel execution. - Stateful = may require memory buffering, reduce efficiency in parallel streams. 4. **Parallel Stream Example** - Demonstrated how stateless ops work efficiently in parallel, but stateful ops like sorted() may disrupt ordering. Why - Many developers use streams without understanding the internal distinction between **stateless (fast, parallel-friendly)** and **stateful (requires global knowledge, potential bottleneck)**. - Highlighting this difference helps optimize code and avoid performance pitfalls when scaling with parallel streams. Logic 1. **Stateless operations** → independent per-element processing, no memory of past/future, efficient for parallel. 2. **Stateful operations** → require knowledge of entire dataset (e.g., deduplication, sorting, limiting). 3. Understanding the distinction is crucial: - For performance tuning (avoid unnecessary stateful ops). - For correctness (some stateful ops disrupt order in parallel). 4. Code examples illustrate real-world behavior: - Stateless: doubling even numbers → efficient, streaming pipeline. - Stateful: removing duplicates + sorting → requires collecting/processing all elements. Key Takeaways ✔ Stateless ops → filter, map, flatMap, peek, forEach. ✔ Stateful ops → distinct, sorted, limit, skip, collect, reduce. ✔ Stateless = efficient, parallel-friendly. ✔ Stateful = may introduce overhead due to buffering/global knowledge. ✔ Always consider operation type when designing stream pipelines, especially for parallel execution. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9094b77 commit e3c4ef2

File tree

2 files changed

+119
-90
lines changed

2 files changed

+119
-90
lines changed

JAVA8/StreamsInJAVA/src/Stateful and Stateless Operations in Java Streams.txt

Lines changed: 0 additions & 90 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
Understanding Stateful and Stateless Operations in Java Streams:
2+
3+
In Java Streams, operations are classified as either **stateless** or **stateful** based on
4+
how they process the data and whether they require maintaining state across elements.
5+
6+
1. Stateless Operations
7+
8+
Definition:
9+
These operations process each element independently, without relying on the elements before
10+
or after it. They do not maintain any internal state.
11+
12+
Examples:
13+
- filter(Predicate) → Filters elements based on a condition.
14+
- map(Function) → Transforms each element.
15+
- flatMap(Function) → Flattens nested collections.
16+
- peek(Consumer) → Performs an action on each element.
17+
- forEach(Consumer) → Iterates over elements.
18+
19+
Example Code:
20+
21+
import java.util.Arrays;
22+
import java.util.List;
23+
24+
public class StatelessExample {
25+
public static void main(String[] args) {
26+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
27+
28+
numbers.stream()
29+
.filter(x -> x % 2 == 0) // Stateless
30+
.map(x -> x * 2) // Stateless
31+
.forEach(System.out::println);
32+
}
33+
}
34+
35+
Output:
36+
4
37+
8
38+
39+
Why Stateless?
40+
- Each element is processed independently without tracking previous or future elements.
41+
42+
---------------------------------------------------------------
43+
2. Stateful Operations
44+
---------------------------------------------------------------
45+
46+
Definition:
47+
These operations require keeping track of previously seen elements or processing the entire
48+
stream before producing output. They may require buffering or sorting.
49+
50+
Examples:
51+
- distinct() → Removes duplicate elements.
52+
- sorted() → Sorts the elements.
53+
- limit(n) → Returns the first n elements.
54+
- skip(n) → Skips the first n elements.
55+
- collect() → Collects results into a collection.
56+
- reduce() → Reduces all elements to a single value.
57+
58+
Example Code:
59+
60+
import java.util.Arrays;
61+
import java.util.List;
62+
63+
public class StatefulExample {
64+
public static void main(String[] args) {
65+
List<Integer> numbers = Arrays.asList(5, 3, 7, 1, 5, 3);
66+
67+
numbers.stream()
68+
.distinct() // Stateful (removes duplicates)
69+
.sorted() // Stateful (needs to sort entire stream)
70+
.forEach(System.out::println);
71+
}
72+
}
73+
74+
Output:
75+
1
76+
3
77+
5
78+
7
79+
80+
Why Stateful?
81+
- distinct() needs to track seen elements to remove duplicates.
82+
- sorted() must process the entire stream to produce an ordered result.
83+
84+
---------------------------------------------------------------
85+
3. Why Does It Matter?
86+
---------------------------------------------------------------
87+
88+
Performance Considerations:
89+
- Stateless operations are faster and process element-by-element.
90+
- Stateful operations may require buffering and increase memory usage.
91+
92+
Parallel Streams:
93+
- Stateless operations are well suited for parallel execution.
94+
- Stateful operations may need synchronization and can degrade performance in parallel streams.
95+
96+
Example: Parallel with Stateless vs Stateful
97+
98+
import java.util.Arrays;
99+
import java.util.List;
100+
101+
public class ParallelExample {
102+
public static void main(String[] args) {
103+
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6);
104+
105+
// Stateless works well in parallel
106+
nums.parallelStream()
107+
.filter(x -> x % 2 == 0)
108+
.forEach(System.out::println);
109+
110+
// Stateful may disrupt order in parallel
111+
nums.parallelStream()
112+
.sorted()
113+
.forEach(System.out::println);
114+
}
115+
}
116+
117+
- Stateless → Independent per-element operations, efficient, good for parallel streams.
118+
- Stateful → Require knowledge of entire stream or previously processed elements, ay reduce efficiency,
119+
especially in parallel.

0 commit comments

Comments
 (0)