Commit 7fa7b79
committed
feat(HowUseLambda): add Employee functional interface and Main with lambda usage
What
- Added Employee interface in package HowUseLambda.
- Defines a single abstract method getName().
- Acts as a functional interface suitable for lambda expressions.
- Added Main class demonstrating lambda usage with Employee.
- Creates Employee E = () -> "Software Engineer".
- Prints result of E.getName().
- Creates another Employee editor = () -> "Editor".
- Prints result of editor.getName().
Why
- Demonstrates how functional interfaces can be implemented with lambda expressions in Java.
- Provides educational example of replacing verbose implementation classes with concise lambdas.
- Shows how interface references can hold lambda expressions, enabling cleaner and more expressive code.
How
- Declared Employee interface with single method getName().
- In Main, used lambda () -> "Software Engineer" to provide implementation at runtime.
- Called getName() on Employee reference to print "Software Engineer".
- Repeated with editor lambda returning "Editor".
Logic
- Inputs: none (hardcoded strings).
- Outputs:
1. "Software Engineer"
2. "Editor"
- Flow:
1. Define Employee E as lambda returning "Software Engineer".
2. Invoke E.getName() → print "Software Engineer".
3. Define Employee editor as lambda returning "Editor".
4. Invoke editor.getName() → print "Editor".
- Edge cases handled:
- Functional interface constraint ensures only one abstract method is present.
- Lambdas provide direct implementation without boilerplate.
- Complexity / performance: O(1) operations, negligible overhead.
- Concurrency / thread-safety:
- Lambdas here are stateless, thus inherently thread-safe.
- Error handling:
- No exceptions thrown in current design.
Real-life applications
- Using functional interfaces for callbacks, event handling, or business logic injection.
- Simplifying code when passing behavior as parameter (e.g., with Java Streams, Runnable, Comparator).
- Replacing anonymous classes with concise lambdas in GUI or concurrent programming.
- Teaching material for introducing functional programming in Java.
Notes
- Employee interface could be annotated with @FunctionalInterface to enforce contract at compile time.
- Example uses hardcoded strings, but lambdas can encapsulate more complex logic or captured variables.
- Lambdas improve readability, reduce boilerplate, and align Java with modern functional programming paradigms.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 895100f commit 7fa7b79
File tree
1 file changed
+14
-0
lines changed- Java 8 Crash Course/Lambda Expression/src/HowUseLambda
1 file changed
+14
-0
lines changedLines changed: 14 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 | + | |
0 commit comments