Skip to content

Commit 9e09c71

Browse files
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

File tree

JAVA8/LambdaExpression/src/explore lambda expressions with collections in Java 8.txt renamed to Java 8 Crash Course/Lambda Expression/src/Lambda expressions.txt

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
explore lambda expressions with collections in Java 8. Here are some practical examples:
1+
Explore lambda expressions with collections in Java 8.
22

3+
Here are some practical examples:
34

4-
1. Using Lambda with forEach()
5+
1. Using Lambda with forEach()
56

67
You can loop through a collection in a concise way using *forEach().
78

@@ -21,18 +22,16 @@ public class LambdaDemo {
2122
}
2223
}
2324

24-
Output:
25-
25+
Output:
2626
Alice
2727
Bob
2828
Charlie
2929

30-
31-
💡 Shortcut: If your lambda only calls a method, use **method reference**:
30+
Shortcut: If your lambda only calls a method, use **method reference**:
3231

3332
names.forEach(System.out::println);
3433

35-
2. Using map() to Transform a List
34+
2. Using map() to Transform a List
3635

3736
You can modify each element using map().
3837

@@ -53,10 +52,9 @@ public class LambdaDemo {
5352
}
5453

5554
Output:
56-
5755
[2, 4, 6, 8]
5856

59-
3. Using filter() to Select Specific Items
57+
3. Using filter() to Select Specific Items
6058

6159
You can filter elements that match a condition.
6260

@@ -77,14 +75,12 @@ public class LambdaDemo {
7775
}
7876

7977
Output:
80-
8178
[10, 40]
8279

83-
4. Using sorted() to Sort a Collection
80+
4. Using sorted() to Sort a Collection
8481

8582
You can sort elements in ascending or custom order.
8683

87-
8884
import java.util.*;
8985
import java.util.stream.*;
9086

@@ -102,7 +98,6 @@ public class LambdaDemo {
10298
}
10399

104100
Output:
105-
106101
[Alice, Bob, Charlie]
107102

108103
Custom Sorting (Descending Order):
@@ -115,15 +110,13 @@ List<Integer> sortedDesc = numbers.stream()
115110
System.out.println(sortedDesc);
116111

117112
Output:
118-
119113
[8, 5, 3, 1]
120114

121115

122-
5. Using reduce() for Aggregation
116+
5. Using reduce() for Aggregation
123117

124118
You can use reduce() to perform calculations like sum or product.
125119

126-
127120
import java.util.*;
128121

129122
public class LambdaDemo {
@@ -138,12 +131,10 @@ public class LambdaDemo {
138131
}
139132
}
140133

141-
142134
Output:
143-
144135
Sum: 15
145136

146-
6. Using removeIf() to Modify Collections
137+
6. Using removeIf() to Modify Collections
147138

148139
You can remove elements that match a condition directly from a collection.
149140

@@ -161,10 +152,9 @@ public class LambdaDemo {
161152
}
162153

163154
Output:
164-
165155
[Bob, Charlie]
166156

167-
7. Using `Comparator` with Lambda
157+
7. Using `Comparator` with Lambda:
168158

169159
You can sort custom objects easily using a lambda expression.
170160

@@ -200,14 +190,12 @@ public class LambdaDemo {
200190
}
201191

202192
Output:
203-
204193
[Bob (25), Alice (30), Charlie (35)]
205194

206-
💡 Summary:
207-
195+
Summary:
208196
- forEach() – Perform actions on each element.
209197
- map() – Transform elements.
210198
- filter() – Select elements matching a condition.
211199
- sorted() – Sort elements.
212200
- reduce() – Combine elements (sum, max, etc.).
213-
- removeIf() – Remove elements from a collection.
201+
- removeIf() – Remove elements from a collection.

0 commit comments

Comments
 (0)