Commit 057bde3
committed
feat(Lambda): add Advanced Java Lambda Expressions tutorial with 10 examples
What
- Added a single-file tutorial (conceptual) covering 10 advanced lambda/functional examples:
1. Returning complex objects from Supplier lambda (Person).
2. Using Comparator with lambda for custom sorting of objects.
3. Chaining Predicates with and(), or(), negate().
4. Function chaining using andThen() and compose().
5. Handling exceptions inside lambdas with try-catch.
6. Parallel streams combined with lambda map/forEach.
7. Defining and using a custom functional interface annotated with @FunctionalInterface.
8. Grouping with Stream.collect(Collectors.groupingBy()).
9. Using Optional with lambda-style callbacks and fallbacks.
10. Combining stream operations (filter, map, reduce) to compute aggregated results.
- Each example includes short code, expected output, and a brief explanation.
Why
- Demonstrates practical, idiomatic uses of Java lambdas and functional APIs beyond trivial examples.
- Teaches common patterns developers need for modern Java: Suppliers, Comparators, Predicates, Functions, Optional, Streams, Collectors, and parallelism.
- Helps readers understand trade-offs (e.g., orElse vs orElseGet, cost of parallel streams, exception handling choices).
- Serves as a compact reference for interviews, onboarding, or quick refreshers.
How
- Present each example as a self-contained code snippet with explanation and sample output.
- Use standard java.util.function interfaces (Supplier, Predicate, Function, Comparator) and java.util.stream APIs.
- Show safe patterns: Optional usage, try-catch inside lambdas for parsing, and prefer method references where concise.
- Call out parallel stream caveats (non-deterministic order, suitability for large, CPU-bound tasks).
- Recommend small pragmatic tips inline (use orElseGet for lazy defaults, prefer Collectors.groupingBy for grouping, annotate custom interfaces with @FunctionalInterface).
Logic
- Inputs: representative hardcoded data per example (lists of Person, integers, strings) to keep demos deterministic.
- Outputs: printed console lines demonstrating behavior (shown with each snippet).
- Flow (typical per example):
1. Create data source (List, Optional, Supplier, etc.).
2. Apply functional operation(s) (map, filter, sort, group, reduce).
3. Collect or consume results (forEach, System.out.println, orElse fallback).
- Edge cases and considerations:
- Exception handling: lambdas cannot declare checked exceptions; use try-catch or wrap checked exceptions.
- Optional: avoid using Optional as fields or in serialization; prefer for return types.
- Parallel streams: beware of thread-safety in lambdas and non-associative operations.
- orElse eagerly evaluates its argument; for expensive defaults use orElseGet.
- Comparator lambdas should avoid subtraction for numeric compare; use Integer.compare to avoid overflow.
- Complexity / performance:
- Most examples are O(n) or O(n log n) depending on sort/collect operations.
- Parallel streams can improve throughput for large datasets but add coordination overhead.
- Concurrency / thread-safety:
- Demonstrate that lambdas are typically stateless; if capturing mutable state, document synchronization needs.
- When using parallelStream, show immutable/side-effect-free operations for correctness.
- Error handling:
- Examples show graceful fallback (e.g., parse fallback to 0) and patterns for safe defaults.
Real-life applications
- Factory patterns and dependency injection using Suppliers.
- Sorting domain objects in UI or reporting pipelines.
- Validations and composite rules using Predicate chains.
- Building ETL or transformation pipelines with Function chaining and Streams.
- Safe parsing of untrusted input with lambda-embedded try-catch.
- Parallel data processing for large collections (analytics, batch jobs).
- Grouping and aggregation for reporting and dashboards using Collectors.
- Using Optional to reduce null-related bugs in service/repository APIs.
Notes
- Prefer method references (Class::method) for readability when possible.
- Annotate custom single-abstract-method interfaces with @FunctionalInterface to make intent explicit.
- Avoid returning Optional from collections or using Optional in fields; use it as a return type from methods.
- For deterministic ordering with parallel operations, ensure reduction is associative and combine functions are correct.
- This tutorial is intentionally example-driven; production code should include tests, benchmarking for parallelism, and careful resource handling.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 0404c71 commit 057bde3
File tree
1 file changed
+23
-46
lines changed- Java 8 Crash Course/Lambda Expression/src
1 file changed
+23
-46
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
| 3 | + | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
| |||
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
27 | | - | |
28 | | - | |
| 25 | + | |
29 | 26 | | |
30 | 27 | | |
31 | | - | |
32 | | - | |
| 28 | + | |
33 | 29 | | |
34 | 30 | | |
35 | 31 | | |
| |||
63 | 59 | | |
64 | 60 | | |
65 | 61 | | |
66 | | - | |
67 | | - | |
| 62 | + | |
68 | 63 | | |
69 | 64 | | |
70 | | - | |
71 | | - | |
| 65 | + | |
72 | 66 | | |
73 | 67 | | |
74 | | - | |
75 | 68 | | |
76 | 69 | | |
77 | 70 | | |
| |||
86 | 79 | | |
87 | 80 | | |
88 | 81 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | 82 | | |
| 83 | + | |
93 | 84 | | |
94 | 85 | | |
95 | 86 | | |
96 | | - | |
97 | | - | |
| 87 | + | |
98 | 88 | | |
99 | 89 | | |
100 | 90 | | |
| |||
111 | 101 | | |
112 | 102 | | |
113 | 103 | | |
114 | | - | |
115 | | - | |
116 | | - | |
| 104 | + | |
117 | 105 | | |
118 | 106 | | |
119 | | - | |
| 107 | + | |
120 | 108 | | |
121 | 109 | | |
122 | 110 | | |
123 | | - | |
124 | 111 | | |
125 | 112 | | |
126 | 113 | | |
| |||
138 | 125 | | |
139 | 126 | | |
140 | 127 | | |
141 | | - | |
142 | | - | |
| 128 | + | |
143 | 129 | | |
144 | 130 | | |
145 | 131 | | |
146 | | - | |
| 132 | + | |
147 | 133 | | |
148 | 134 | | |
149 | 135 | | |
| |||
160 | 146 | | |
161 | 147 | | |
162 | 148 | | |
163 | | - | |
164 | | - | |
| 149 | + | |
165 | 150 | | |
166 | 151 | | |
167 | 152 | | |
168 | 153 | | |
169 | 154 | | |
170 | 155 | | |
171 | 156 | | |
172 | | - | |
173 | | - | |
| 157 | + | |
174 | 158 | | |
175 | 159 | | |
176 | 160 | | |
| |||
186 | 170 | | |
187 | 171 | | |
188 | 172 | | |
189 | | - | |
190 | | - | |
| 173 | + | |
191 | 174 | | |
192 | 175 | | |
193 | | - | |
194 | | - | |
| 176 | + | |
195 | 177 | | |
196 | 178 | | |
197 | 179 | | |
| |||
223 | 205 | | |
224 | 206 | | |
225 | 207 | | |
226 | | - | |
227 | | - | |
| 208 | + | |
228 | 209 | | |
229 | 210 | | |
230 | | - | |
231 | | - | |
| 211 | + | |
232 | 212 | | |
233 | 213 | | |
234 | 214 | | |
| |||
245 | 225 | | |
246 | 226 | | |
247 | 227 | | |
248 | | - | |
249 | | - | |
| 228 | + | |
250 | 229 | | |
251 | 230 | | |
252 | | - | |
| 231 | + | |
253 | 232 | | |
254 | 233 | | |
255 | 234 | | |
| |||
269 | 248 | | |
270 | 249 | | |
271 | 250 | | |
272 | | - | |
273 | | - | |
| 251 | + | |
274 | 252 | | |
275 | 253 | | |
276 | | - | |
277 | | - | |
| 254 | + | |
278 | 255 | | |
279 | 256 | | |
280 | 257 | | |
281 | 258 | | |
282 | 259 | | |
283 | | - | |
| 260 | + | |
0 commit comments