-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamPipelineDemo.java
More file actions
134 lines (108 loc) · 4.86 KB
/
Copy pathStreamPipelineDemo.java
File metadata and controls
134 lines (108 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.*;
import java.util.stream.*;
/**
* Demonstrates declarative Stream Pipelines in modern Java
* Key concepts: Functional programming, lazy evaluation, terminal operations
*/
public class StreamPipelineDemo {
public static void main(String[] args) {
System.out.println("=== Stream Pipeline Demo ===\n");
demonstrateBasicOperations();
demonstrateComplexPipeline();
demonstrateLazyEvaluation();
demonstrateCollectors();
}
private static void demonstrateBasicOperations() {
System.out.println("1. Basic Stream Operations");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Imperative approach
System.out.println(" Imperative (old way):");
List<Integer> evenSquares = new ArrayList<>();
for (Integer num : numbers) {
if (num % 2 == 0) {
evenSquares.add(num * num);
}
}
System.out.println(" Even squares: " + evenSquares);
// Declarative approach with streams
System.out.println("\n Declarative (streams):");
List<Integer> evenSquaresStream = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println(" Even squares: " + evenSquaresStream);
System.out.println(" ✓ More readable, expresses WHAT not HOW\n");
}
private static void demonstrateComplexPipeline() {
System.out.println("2. Complex Pipeline Example");
List<Employee> employees = Arrays.asList(
new Employee("Alice", "Engineering", 75000),
new Employee("Bob", "Engineering", 85000),
new Employee("Charlie", "Sales", 65000),
new Employee("David", "Engineering", 95000),
new Employee("Eve", "Sales", 70000));
System.out.println(" Find top 2 Engineering salaries:");
List<String> topEngineers = employees.stream()
.filter(e -> e.department.equals("Engineering"))
.sorted(Comparator.comparing(e -> -e.salary))
.limit(2)
.map(e -> e.name + " ($" + e.salary + ")")
.collect(Collectors.toList());
topEngineers.forEach(e -> System.out.println(" - " + e));
System.out.println();
}
private static void demonstrateLazyEvaluation() {
System.out.println("3. Lazy Evaluation");
System.out.println(" Streams don't execute until terminal operation\n");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
System.out.println(" Creating stream pipeline (no output yet):");
Stream<Integer> stream = numbers.stream()
.peek(n -> System.out.println(" - Filtering: " + n))
.filter(n -> n % 2 == 0)
.peek(n -> System.out.println(" - Mapping: " + n))
.map(n -> n * n);
System.out.println("\n Now calling terminal operation (collect):");
List<Integer> result = stream.collect(Collectors.toList());
System.out.println(" Result: " + result);
System.out.println(" ✓ Operations executed only when needed\n");
}
private static void demonstrateCollectors() {
System.out.println("4. Powerful Collectors");
List<Employee> employees = Arrays.asList(
new Employee("Alice", "Engineering", 75000),
new Employee("Bob", "Engineering", 85000),
new Employee("Charlie", "Sales", 65000),
new Employee("David", "Engineering", 95000),
new Employee("Eve", "Sales", 70000));
// Group by department
Map<String, List<Employee>> byDept = employees.stream()
.collect(Collectors.groupingBy(e -> e.department));
System.out.println(" Employees grouped by department:");
byDept.forEach((dept, emps) -> {
System.out.println(" " + dept + ": " +
emps.stream().map(e -> e.name).collect(Collectors.joining(", ")));
});
// Average salary by department
Map<String, Double> avgSalary = employees.stream()
.collect(Collectors.groupingBy(
e -> e.department,
Collectors.averagingDouble(e -> e.salary)));
System.out.println("\n Average salary by department:");
avgSalary.forEach((dept, avg) -> System.out.printf(" %s: $%.2f%n", dept, avg));
System.out.println();
}
}
class Employee {
String name;
String department;
double salary;
Employee(String name, String department, double salary) {
this.name = name;
this.department = department;
this.salary = salary;
}
@Override
public String toString() {
return name + " (" + department + ", $" + salary + ")";
}
}