Skip to content

Commit e93d2fe

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

1 file changed

+18
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
Java Lambda Expression Cheat Sheet
22

3-
Basic Syntax:
3+
Basic Syntax:
44

55
(parameters) -> { body }
66

7-
1. No Parameters
7+
1. No Parameters
88

99
Runnable r = () -> System.out.println("Hello, World!");
1010

11-
✅ **Explanation:
12-
11+
Explanation:
1312
- () → No parameters.
1413
- -> → Lambda arrow.
1514
- System.out.println() → Action (body).
1615

17-
18-
✅ 2. Single Parameter
16+
2. Single Parameter
1917

2018
Consumer<String> printer = name -> System.out.println(name);
2119
printer.accept("Alice");
2220

2321

24-
Explanation:
22+
Explanation:
2523
- One parameter doesn't need parentheses.
2624
- Uses the Consumer functional interface to print.
2725

28-
29-
✅ 3. Multiple Parameters
26+
3. Multiple Parameters
3027

3128
BiFunction<Integer, Integer, Integer> sum = (a, b) -> a + b;
3229
System.out.println(sum.apply(5, 10));
3330

3431

35-
✅ Explanation:
36-
32+
Explanation:
3733
- Multiple parameters must be in parentheses.
3834
- BiFunction takes two inputs and returns a result.
3935

40-
4. With Code Block (Multiple Statements)
36+
4. With Code Block (Multiple Statements)
4137

4238
BiFunction<Integer, Integer, Integer> sum = (a, b) -> {
4339
int result = a + b;
@@ -46,26 +42,23 @@ BiFunction<Integer, Integer, Integer> sum = (a, b) -> {
4642
};
4743
sum.apply(5, 10);
4844

49-
✅ Explanation:
50-
45+
Explanation:
5146
- Use curly braces {} when the body has more than one statement.
5247
- Must use return if the method has a return type.
5348

5449

55-
✅ 5. Using Method Reference (Shortcut for Lambdas):
56-
50+
5. Using Method Reference (Shortcut for Lambdas):
5751
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
5852
names.forEach(System.out::println);
5953

60-
61-
✅ Explanation:
62-
54+
Explanation:
6355
- System.out::println is a method reference —a cleaner way to call existing methods.
6456
- Equivalent to:
6557

6658
names.forEach(name -> System.out.println(name));
6759

68-
✅ 6. With Functional Interfaces
60+
61+
6. With Functional Interfaces
6962

7063
| Functional Interface | Parameters | Return Type | Example Lambda |
7164
|----------------------------|------------|-------------|---------------------------------|
@@ -77,7 +70,7 @@ names.forEach(System.out::println);
7770
| Predicate<T> | 1 | boolean | x -> x > 10 |
7871

7972

80-
7. Common Examples:
73+
7. Common Examples:
8174

8275
1.Filter a List (with filter())
8376

@@ -96,23 +89,23 @@ List<String> upperCaseNames = names.stream()
9689
System.out.println(upperCaseNames);
9790

9891

99-
3. Sort a List
92+
3. Sort a List:
10093

10194
List<String> names = Arrays.asList("Charlie", "Alice", "Bob");
10295
names.sort((a, b) -> a.compareTo(b));
10396
System.out.println(names);
10497

105-
4. Sum of Numbers (with reduce())
98+
4. Sum of Numbers (with reduce()):
10699

107100
List<Integer> numbers = Arrays.asList(1, 2, 3);
108101
int sum = numbers.stream()
109102
.reduce(0, (a, b) -> a + b);
110103
System.out.println(sum);
111104

112-
🎯 Quick Rules:
113105

106+
Quick Rules:
114107
1. 0 Parameters: () -> System.out.println("Hello");
115108
2. 1 Parameter: x -> x * 2;
116109
3. Multiple Parameters: (a, b) -> a + b;
117110
4. Return Statement: Use {} if there are **multiple statements.
118-
5. Method Reference: Use ClassName::methodName for simple method calls.
111+
5. Method Reference: Use ClassName::methodName for simple method calls.

0 commit comments

Comments
 (0)