Skip to content

Commit fa103ae

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

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
🔹 What is a Functional Interface?
2+
• An interface with exactly one abstract method.
3+
• Can have any number of default or static methods.
4+
• Acts as a contract for lambda expressions or method references.
5+
• Annotated with @FunctionalInterface (optional but recommended).
6+
7+
8+
🔹 How to Create a Functional Interface?
9+
1. Use the interface keyword.
10+
2. Define only one abstract method inside it.
11+
3. (Optional) Add @FunctionalInterface annotation to enforce compiler checks.
12+
13+
14+
15+
🔹 General Syntax:
16+
@FunctionalInterface
17+
interface InterfaceName {
18+
ReturnType methodName(ParameterType parameters);
19+
20+
// You may also define default/static methods
21+
default void helperMethod() { ... }
22+
static void utilityMethod() { ... }
23+
}
24+
25+
26+
27+
🔹 Short Example:
28+
@FunctionalInterface
29+
interface MyFunctionalInterface {
30+
void sayMessage(String msg); // Single Abstract Method
31+
}
32+
33+
public class Package.Package2.Test {
34+
public static void main(String[] args) {
35+
// Using lambda expression
36+
MyFunctionalInterface message = (msg) -> System.out.println("Hello, " + msg);
37+
message.sayMessage("Java");
38+
}
39+
}
40+
41+
42+
43+
Quick Recap:
44+
• What? Interface with one abstract method.
45+
• How? Use @FunctionalInterface, define one abstract method.
46+
• Syntax: ReturnType methodName(ParameterType parameters);
47+
• Usage: Ideal with lambda expressions for cleaner, concise code.
48+
49+
50+
Example of `Function<T, R>` in Java 8:
51+
52+
- `Function<T, R>` is a functional interface from `java.util.function` package.
53+
- `T` is the input type (`String` in this case).
54+
- `R` is the return type (`Integer` in this case).
55+
- The lambda expression (`str -> str.length()`) defines the function body.
56+
- `.apply("Java 8")` executes the function.
57+
58+
59+
Function<String, Integer> func = str -> str.length();
60+
System.out.println(func.apply("Java 8")); // Output: 6
61+
62+
Other Useful Java 8 Functional Interfaces:
63+
64+
| Functional Interface| Description |
65+
|---------------------|-----------------------------------------|
66+
| `Function<T, R>` | Takes an argument `T` and returns `R`. |
67+
| `Predicate<T>` | Returns `boolean` (used for filtering). |
68+
| `Consumer<T>` | Consumes `T` without returning a value. |
69+
| `Supplier<T>` | Supplies a value of `T` (no input). |

0 commit comments

Comments
 (0)