Commit fa103ae
committed
docs(functional-interfaces): explain functional interfaces and demonstrate Function<T,R> usage
What
- Added documentation explaining the concept of functional interfaces:
- Interface with exactly one abstract method.
- Can include any number of default and static methods.
- Typically annotated with @FunctionalInterface for compiler enforcement.
- Included step-by-step guide:
1. How to create a functional interface.
2. General syntax with abstract, default, and static methods.
3. Example of a custom functional interface with lambda usage.
- Added example of built-in `Function<T,R>`:
- Demonstrates mapping String → Integer by computing string length.
- Showcased use of `apply()` method.
Why
- Functional interfaces are the foundation of Java 8’s functional programming model.
- Enables usage of lambdas and method references as concise implementations.
- Clarifies the role of built-in functional interfaces (`Function`, `Predicate`, `Consumer`, `Supplier`).
Logic
1. Created custom functional interface `MyFunctionalInterface` with a single abstract method:
- `void sayMessage(String msg)`
- Used lambda `(msg) -> System.out.println("Hello, " + msg)` to provide implementation.
2. Demonstrated `Function<String,Integer>`:
- `Function<String, Integer> func = str -> str.length();`
- Invoked `func.apply("Java 8")`, output: `6`.
Real-life applications
- Functional interfaces are widely used in:
- Stream API (e.g., `.map`, `.filter`, `.forEach`).
- Event handling (callbacks, listeners).
- Utility abstractions where behavior is passed as a parameter.
- `Function<T,R>` commonly used for:
- Data transformations (DTO mapping).
- String/number parsing and conversions.
- Extracting derived fields in collections.
Notes
- Only **one abstract method** is allowed; default/static methods do not violate the rule.
- `@FunctionalInterface` is optional but recommended for compiler-level validation.
- Built-in functional interfaces in `java.util.function` (Function, Predicate, Consumer, Supplier) cover most functional programming scenarios.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5315371 commit fa103ae
File tree
1 file changed
+69
-0
lines changed- Java 8 Crash Course/Functional Interface/src
1 file changed
+69
-0
lines changedLines changed: 69 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 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
0 commit comments