Skip to content

Commit 3054aa5

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

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
🔹 What is a Lambda Expression?
2+
3+
A lambda expression in Java is a short, anonymous function that you can use in place of writing a full method.
4+
It’s mainly used to provide implementation of functional interfaces (interfaces with a single abstract method).
5+
6+
7+
🔹 How to Create a Lambda Expression (Step by Step)
8+
1. Start from a normal method
9+
• Normally, a method in Java has:
10+
• access modifier (public, private, etc.)
11+
• return type (int, void, etc.)
12+
• method name
13+
• parameter list in brackets ( )
14+
• method body inside { }
15+
16+
2. Remove modifiers and method name
17+
• In a lambda expression, you don’t need public, static, or even the method name.
18+
• The lambda will be treated as an implementation of the abstract method.
19+
20+
3. Keep only parameters
21+
• The parameters of the abstract method remain.
22+
• Example: if the method takes (int a, int b), you keep (a, b).
23+
24+
4. Insert the arrow operator ->
25+
• Place -> after the parameter list.
26+
• This arrow separates the input parameters from the implementation body.
27+
28+
5. Write the body
29+
• After the arrow, write the method body.
30+
• If the body has only one line of code, you don’t need curly braces { }.
31+
• If it has multiple statements, use { } to group them.
32+
33+
6. Return keyword is optional for single expressions
34+
• If your body is just a single expression that returns a value, you can skip return.
35+
• Java will automatically return that value.
36+
37+
38+
🔹 General Syntax
39+
(parameters) -> { body }
40+
41+
42+
43+
🔹 Variations
44+
• No parameters: use empty parentheses () -> { body }
45+
• One parameter: parentheses can be omitted x -> { body }
46+
• Multiple parameters: must use parentheses (a, b) -> { body }
47+
• Single statement body: no braces or return needed
48+
• Multi-statement body: braces { } required
49+
50+
51+
52+
In short:
53+
Take the method → drop modifiers + return type + method name → keep only parameters → insert -> → then write the body.
54+
55+

0 commit comments

Comments
 (0)