|
| 1 | +--- |
| 2 | +Title: Functional Interfaces in Java |
| 3 | +layout: post |
| 4 | +title: Functional Interfaces in Java |
| 5 | +--- |
| 6 | +In Java, interfaces that contain only a single method are called **"Functional Interfaces."** These are designed to serve as the **target for lambda expressions** or method references. |
| 7 | + |
| 8 | +A **Functional Interface** has exactly one abstract method, although it may include default or static methods. One of the most well-known examples is the `java.lang.Runnable` interface, which defines only the `run()` method. |
| 9 | + |
| 10 | +Since Java 8, the `@FunctionalInterface` annotation can be used to explicitly mark an interface as functional. While this annotation is optional, it helps the compiler ensure that the interface contains only one abstract method. |
| 11 | + |
| 12 | +### Example: |
| 13 | +```java |
| 14 | +@FunctionalInterface |
| 15 | +public interface MyFunctionalInterface { |
| 16 | + void doSomething(); |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +### Common Functional Interfaces in Java: |
| 21 | +- `java.util.function.Consumer<T>` (takes one argument and returns nothing) |
| 22 | +- `java.util.function.Function<T, R>` (takes one argument and returns a result) |
| 23 | +- `java.util.function.Predicate<T>` (takes one argument and returns a boolean) |
| 24 | + |
| 25 | +With the introduction of Functional Interfaces in Java 8, lambda expressions and method references became possible, allowing for more concise and readable code. |
| 26 | + |
| 27 | +Here’s an example of how you can use a **Functional Interface** with a **lambda expression** in Java. This example demonstrates a simple use case where we define and use a functional interface. |
| 28 | + |
| 29 | +### Example of a Functional Interface with Lambda Expression |
| 30 | + |
| 31 | +```java |
| 32 | +@FunctionalInterface |
| 33 | +interface Greeting { |
| 34 | + void sayHello(String name); |
| 35 | +} |
| 36 | + |
| 37 | +public class Main { |
| 38 | + public static void main(String[] args) { |
| 39 | + // Using a lambda expression to implement the Functional Interface |
| 40 | + Greeting greeting = (name) -> System.out.println("Hello, " + name + "!"); |
| 41 | + |
| 42 | + // Invoking the method defined in the functional interface |
| 43 | + greeting.sayHello("Alice"); |
| 44 | + greeting.sayHello("Bob"); |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Explanation: |
| 50 | + |
| 51 | +1. **Functional Interface**: The `Greeting` interface is a functional interface because it contains only one abstract method, `sayHello(String name)`. The `@FunctionalInterface` annotation ensures that the interface conforms to the definition of a functional interface. |
| 52 | + |
| 53 | +2. **Lambda Expression**: In the `main` method, a lambda expression `(name) -> System.out.println("Hello, " + name + "!")` is used to provide an implementation of the `sayHello` method. |
| 54 | + |
| 55 | +3. **Calling the Lambda**: Once we assign the lambda to a variable of type `Greeting`, we can use it like any other method. Here, `greeting.sayHello("Alice")` calls the lambda expression and prints "Hello, Alice!". |
| 56 | + |
| 57 | +### Output: |
| 58 | +``` |
| 59 | +Hello, Alice! |
| 60 | +Hello, Bob! |
| 61 | +``` |
| 62 | + |
| 63 | +This example shows how functional interfaces in Java, combined with lambda expressions, can simplify code and make it more readable and expressive. |
0 commit comments