Commit 9e09c71
committed
docs(Lambda): add practical examples of using lambda expressions with collections
What
- Documented how to explore Java 8 lambda expressions with collections and streams.
- Added practical examples with code and outputs:
1. forEach() with lambdas and method references.
2. map() to transform elements in a list.
3. filter() to select items by condition.
4. sorted() for natural and custom sorting (ascending/descending).
5. reduce() for aggregations (e.g., sum).
6. removeIf() to modify collections directly.
7. Comparator with lambda for sorting custom objects.
- Included summary of common operations:
- forEach, map, filter, sorted, reduce, removeIf.
Why
- Provides real-world, hands-on examples of how lambdas simplify collection handling.
- Shows how lambdas integrate seamlessly with Streams API.
- Helps developers learn functional-style programming by applying it to familiar tasks (iteration, filtering, aggregation).
- Encourages using declarative style instead of verbose loops and conditionals.
How
- Each example:
- Uses simple collections (List<String>, List<Integer>, custom Person objects).
- Demonstrates traditional Java code vs lambda equivalent.
- Prints results to verify correctness.
- Introduced method references (System.out::println) as shorthand for simple lambdas.
- Used Collectors.toList() for collecting stream results.
- Provided examples for both primitive operations (sum, even filtering) and object-based operations (custom sorting).
Logic
- Inputs: sample lists of strings, integers, and custom Person objects.
- Outputs: transformed, filtered, sorted, or aggregated results printed to stdout.
- Flow per example:
1. Construct a collection.
2. Apply a lambda expression with a stream or collection method.
3. Collect or print results.
- Edge cases:
- reduce() requires an identity value for empty collections.
- removeIf modifies collection in-place (use caution with concurrent iteration).
- Custom comparators must avoid integer overflow; prefer Integer.compare.
- Complexity / performance:
- forEach, map, filter, sorted, reduce run in O(n) or O(n log n) depending on operation.
- Stream pipelines are lazy; only terminal operations trigger evaluation.
- Concurrency / thread-safety:
- Streams can be parallelized with parallelStream() for large datasets.
- Lambdas must remain stateless for correctness in parallel pipelines.
- Error handling:
- If lambdas throw exceptions, they must be caught within the expression.
Real-life applications
- Filtering user data (e.g., selecting active users).
- Transforming datasets (e.g., normalizing names to uppercase).
- Sorting entities by attributes (e.g., by age, by name).
- Aggregating metrics (e.g., total sales, average rating).
- Cleaning collections (remove invalid or unwanted elements).
- Declaratively expressing business logic in fewer lines of code.
Notes
- Use method references for cleaner syntax where lambdas only call existing methods.
- Streams are more powerful than simple collection loops, offering chaining of operations.
- Prefer immutable transformations with streams instead of mutating state.
- removeIf is useful for in-place modifications but should be used carefully in concurrent contexts.
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent 3054aa5 commit 9e09c71
1 file changed
Lines changed: 13 additions & 25 deletions
Lines changed: 13 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 | | |
| |||
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | | - | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
| 30 | + | |
32 | 31 | | |
33 | 32 | | |
34 | 33 | | |
35 | | - | |
| 34 | + | |
36 | 35 | | |
37 | 36 | | |
38 | 37 | | |
| |||
53 | 52 | | |
54 | 53 | | |
55 | 54 | | |
56 | | - | |
57 | 55 | | |
58 | 56 | | |
59 | | - | |
| 57 | + | |
60 | 58 | | |
61 | 59 | | |
62 | 60 | | |
| |||
77 | 75 | | |
78 | 76 | | |
79 | 77 | | |
80 | | - | |
81 | 78 | | |
82 | 79 | | |
83 | | - | |
| 80 | + | |
84 | 81 | | |
85 | 82 | | |
86 | 83 | | |
87 | | - | |
88 | 84 | | |
89 | 85 | | |
90 | 86 | | |
| |||
102 | 98 | | |
103 | 99 | | |
104 | 100 | | |
105 | | - | |
106 | 101 | | |
107 | 102 | | |
108 | 103 | | |
| |||
115 | 110 | | |
116 | 111 | | |
117 | 112 | | |
118 | | - | |
119 | 113 | | |
120 | 114 | | |
121 | 115 | | |
122 | | - | |
| 116 | + | |
123 | 117 | | |
124 | 118 | | |
125 | 119 | | |
126 | | - | |
127 | 120 | | |
128 | 121 | | |
129 | 122 | | |
| |||
138 | 131 | | |
139 | 132 | | |
140 | 133 | | |
141 | | - | |
142 | 134 | | |
143 | | - | |
144 | 135 | | |
145 | 136 | | |
146 | | - | |
| 137 | + | |
147 | 138 | | |
148 | 139 | | |
149 | 140 | | |
| |||
161 | 152 | | |
162 | 153 | | |
163 | 154 | | |
164 | | - | |
165 | 155 | | |
166 | 156 | | |
167 | | - | |
| 157 | + | |
168 | 158 | | |
169 | 159 | | |
170 | 160 | | |
| |||
200 | 190 | | |
201 | 191 | | |
202 | 192 | | |
203 | | - | |
204 | 193 | | |
205 | 194 | | |
206 | | - | |
207 | | - | |
| 195 | + | |
208 | 196 | | |
209 | 197 | | |
210 | 198 | | |
211 | 199 | | |
212 | 200 | | |
213 | | - | |
| 201 | + | |
0 commit comments