Skip to content

Commit 32a23f1

Browse files
committed
feat(streams): demonstrate parallelStream transformations with map, filter, distinct, and sorted
What - Added `Test.java` showcasing multiple parallel stream operations: - Squaring numbers (`map(x -> x*x)`). - Filtering even numbers (`filter(x -> x % 2 == 0)`). - Dividing even numbers by 2 (`map(x -> x/2)`). - Combining `map`, `distinct`, and `sorted` for ordered unique results. - Collected results using `Collectors.toList()` for easy inspection. Why - Parallel streams in Java 8+ allow data processing across multiple CPU cores with minimal code changes. - Demonstrates common transformations (`map`, `filter`, `distinct`, `sorted`) under parallel execution. - Reinforces difference between declarative, functional pipeline vs imperative loops. Logic 1. **Squaring numbers** - Input: `[1..10]` - Pipeline: `parallelStream().map(x -> x*x)` - Output: `[1,4,9,16,25,36,49,64,81,100]` 2. **Filtering even numbers** - Predicate: `x % 2 == 0` - Output: `[2,4,6,8,10]` 3. **Custom transformation (divide evens by 2)** - Filter evens, then `map(x -> x/2)` - Output: `[1,2,3,4,5]` 4. **Distinct + sorted after doubling** - Multiply each element by 2 - Ensure uniqueness with `distinct()` - Order results with `sorted()` - Output: `[2,4,6,8,10,12,14,16,18,20]` Key points - `parallelStream()` automatically partitions work across CPU cores. - Order of elements in intermediate pipelines may vary, but `sorted()` restores deterministic ordering. - `distinct()` ensures uniqueness (useful for deduplication in parallel pipelines). - Collectors unify results into a single collection after parallel execution. Real-world applications - Squaring/transformation → numerical simulations, matrix computations. - Filtering → selecting relevant DB/API results in multi-core processing. - Distinct + sorted → deduplication and ordering of large log or transaction datasets. - Parallel pipelines → speed up analytics in back-end systems with large data sets. Notes - Parallel streams use the common ForkJoinPool with `Runtime.getRuntime().availableProcessors()` threads. - Avoid side effects or shared mutable state in lambdas; correctness may break under parallel execution. - Best for CPU-intensive, independent tasks on medium-to-large datasets. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 0e8b360 commit 32a23f1

File tree

1 file changed

+46
-0
lines changed
  • Java 8 Crash Course/Java 8 Streams/Parallel Streams/src

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
import java.util.stream.Collectors;
4+
5+
public class Test {
6+
public static void main(String[] args) {
7+
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
8+
9+
// sing parallelStream() → parallel processing with multiple threads.
10+
// By default, the number of threads = no. of available CPU cores.
11+
List<Integer> squaredParallel =
12+
list.parallelStream()
13+
.map(x -> x * x) // Each element → squared
14+
.collect(Collectors.toList());
15+
16+
System.out.println("Squares (Parallel): " + squaredParallel);
17+
18+
// Filtering even numbers in parallel
19+
List<Integer> evenParallel =
20+
list.parallelStream()
21+
.filter(x -> x % 2 == 0)
22+
.collect(Collectors.toList());
23+
24+
System.out.println("Even Numbers (Parallel): " + evenParallel);
25+
26+
// Custom transformation → Divide even numbers by 2
27+
List<Integer> dividedParallel =
28+
list.parallelStream()
29+
.filter(x -> x % 2 == 0)
30+
.map(x -> x / 2)
31+
.collect(Collectors.toList());
32+
33+
System.out.println("Even Numbers ÷ 2 (Parallel): " + dividedParallel);
34+
35+
// Distinct + Sorted (Parallel)
36+
List<Integer> distinctSortedParallel =
37+
list.parallelStream()
38+
.map(x -> x * 2) // Multiply by 2
39+
.distinct()
40+
.sorted()
41+
.collect(Collectors.toList());
42+
43+
System.out.println("Distinct + Sorted (Parallel): " + distinctSortedParallel);
44+
}
45+
}
46+

0 commit comments

Comments
 (0)