Commit 3054aa5
committed
docs(Lambda): add explanation of what a lambda expression is and how to create one
What
- Documented definition, syntax, and creation steps for Java lambda expressions.
- Covered:
- Definition: short, anonymous function used to implement functional interfaces.
- Step-by-step process for creating a lambda:
1. Start with a normal method signature.
2. Remove access modifiers and method name.
3. Keep only parameters.
4. Insert arrow operator (->).
5. Add body (single or multi-statement).
6. Note: return keyword optional for single-expression bodies.
- General syntax: (parameters) -> { body }.
- Variations:
- No parameters → () -> { body }.
- One parameter → x -> { body } (parentheses optional).
- Multiple parameters → (a, b) -> { body }.
- Single statement → no braces or return needed.
- Multi-statement → braces { } required.
- Short summary: Take method → drop modifiers, return type, and name → keep parameters → insert -> → write body.
Why
- Provides a foundational explanation for developers new to lambdas.
- Clarifies how lambdas differ from traditional method definitions.
- Makes the transition from verbose anonymous classes to concise lambdas easier.
- Helps establish mental model: parameters → arrow → body.
How
- Explained construction through sequential simplification of a method into a lambda.
- Used examples for no parameters, one parameter, multiple parameters.
- Highlighted rules for braces and return keyword depending on body type.
- Provided "in short" section as a memory aid.
Logic
- Inputs: functional interface method definition (single abstract method).
- Outputs: lambda expression implementing that method.
- Flow:
1. Identify target method signature.
2. Strip away modifiers and method name.
3. Place arrow (->) between parameters and body.
4. Simplify syntax depending on body length and parameter count.
- Edge cases:
- Parentheses mandatory for zero or multiple parameters, optional for one.
- Braces required when body has multiple statements.
- Complexity / performance: lambdas compiled to invokedynamic bytecode; lighter than anonymous classes.
- Concurrency / thread-safety:
- Stateless lambdas are inherently safe.
- Captured variables must be effectively final.
- Error handling:
- Checked exceptions not supported directly; must wrap or use custom functional interfaces.
Real-life applications
- Implementing callbacks and listeners without boilerplate.
- Writing concise comparators for sorting.
- Using lambdas in Streams API (map, filter, reduce).
- Passing behavior as parameter in higher-order functions.
Notes
- Always use @FunctionalInterface annotation when defining custom single-method interfaces to clarify lambda compatibility.
- Prefer method references (Class::method) for clarity when lambda only calls an existing method.
- Keep lambdas concise for readability; extract complex logic into named methods.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e93d2fe commit 3054aa5
File tree
1 file changed
+55
-0
lines changed- Java 8 Crash Course/Lambda Expression/src
1 file changed
+55
-0
lines changedLines changed: 55 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
0 commit comments