Commit e93d2fe
committed
docs(Lambda): add Java Lambda Expression Cheat Sheet
What
- Added concise cheat sheet summarizing Java lambda expression syntax and usage.
- Covered:
1. Basic syntax: (parameters) -> { body }.
2. No parameters example with Runnable.
3. Single parameter example with Consumer.
4. Multiple parameters example with BiFunction.
5. Multi-statement body with return.
6. Method references as shorthand for lambdas.
7. Common functional interfaces table (Runnable, Consumer, Supplier, Function, BiFunction, Predicate).
8. Practical stream examples:
- Filtering a list.
- Mapping values to new form.
- Sorting collections.
- Reducing numbers to a sum.
- Added quick rules for parameter/return usage and method reference shorthand.
Why
- Serves as a quick reference for developers learning or revisiting lambda expressions.
- Reinforces patterns by combining short examples with explanations.
- Provides mapping between common functional interfaces and their lambda usage.
- Connects lambdas with Streams API for practical real-world applications.
How
- Organized content into numbered sections for clarity.
- Used code snippets with comments to illustrate each case.
- Provided explanation alongside code for direct understanding.
- Added tabular format for functional interfaces to show parameters, return type, and example lambda.
Logic
- Inputs: various code constructs (Runnable, Consumer, Function, etc.).
- Outputs: console prints or computed values demonstrating lambda usage.
- Flow:
1. Define functional interface type.
2. Assign lambda or method reference to it.
3. Execute interface method to demonstrate effect.
- Edge cases:
- Multi-statement lambdas require braces and explicit return.
- Method references only work when lambda directly calls an existing method.
- Complexity / performance: lambdas compile to invokedynamic calls; more efficient than anonymous classes.
- Concurrency / thread-safety:
- Stateless lambdas are inherently thread-safe.
- Captured variables must be effectively final.
- Error handling:
- Checked exceptions not directly supported in most functional interfaces; must wrap inside lambda.
Real-life applications
- Replacing verbose anonymous classes with compact lambdas for listeners and callbacks.
- Functional-style operations on collections with Streams (map, filter, reduce).
- Simplified comparator definitions for sorting.
- Building concise pipelines in data processing, logging, and validation.
Notes
- Prefer method references (Class::method) where possible for readability.
- Use @FunctionalInterface annotation when creating custom single-method interfaces.
- Keep lambdas short and clear; move complex logic to named methods for maintainability.
- Cheat sheet can be expanded with advanced usage (compose, andThen, parallelStream) for completeness.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent ceab40b commit e93d2fe
File tree
1 file changed
+18
-25
lines changed- Java 8 Crash Course/Lambda Expression/src
1 file changed
+18
-25
lines changedLines changed: 18 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
12 | | - | |
| 11 | + | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | | - | |
18 | | - | |
| 16 | + | |
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
24 | | - | |
| 22 | + | |
25 | 23 | | |
26 | 24 | | |
27 | 25 | | |
28 | | - | |
29 | | - | |
| 26 | + | |
30 | 27 | | |
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
34 | 31 | | |
35 | | - | |
36 | | - | |
| 32 | + | |
37 | 33 | | |
38 | 34 | | |
39 | 35 | | |
40 | | - | |
| 36 | + | |
41 | 37 | | |
42 | 38 | | |
43 | 39 | | |
| |||
46 | 42 | | |
47 | 43 | | |
48 | 44 | | |
49 | | - | |
50 | | - | |
| 45 | + | |
51 | 46 | | |
52 | 47 | | |
53 | 48 | | |
54 | 49 | | |
55 | | - | |
56 | | - | |
| 50 | + | |
57 | 51 | | |
58 | 52 | | |
59 | 53 | | |
60 | | - | |
61 | | - | |
62 | | - | |
| 54 | + | |
63 | 55 | | |
64 | 56 | | |
65 | 57 | | |
66 | 58 | | |
67 | 59 | | |
68 | | - | |
| 60 | + | |
| 61 | + | |
69 | 62 | | |
70 | 63 | | |
71 | 64 | | |
| |||
77 | 70 | | |
78 | 71 | | |
79 | 72 | | |
80 | | - | |
| 73 | + | |
81 | 74 | | |
82 | 75 | | |
83 | 76 | | |
| |||
96 | 89 | | |
97 | 90 | | |
98 | 91 | | |
99 | | - | |
| 92 | + | |
100 | 93 | | |
101 | 94 | | |
102 | 95 | | |
103 | 96 | | |
104 | 97 | | |
105 | | - | |
| 98 | + | |
106 | 99 | | |
107 | 100 | | |
108 | 101 | | |
109 | 102 | | |
110 | 103 | | |
111 | 104 | | |
112 | | - | |
113 | 105 | | |
| 106 | + | |
114 | 107 | | |
115 | 108 | | |
116 | 109 | | |
117 | 110 | | |
118 | | - | |
| 111 | + | |
0 commit comments