A function interface is an interface with exactly one abstract method. Function interfaces are used extensively in lambda expressions and method statements to assign values in contexts that can be efficiently implemented. They form the basis for programming features introduced in Java 8 and later.
Characteristics of Functional Interfaces
Single Abstract Method (SAM) Function interfaces have exactly one abstract method that requires a lambda expression or method reference.
Default and static methods Functional interfaces can have default methods (methods with default implementations) and static methods (utility methods).
Annotation @FunctionalInterface Although not mandatory, annotating an interface with
@FunctionalInterface clearly indicates that it is intended to be used as a functional interface If the interface does not meet the requirements of a functional interface, the compiler issues an error they.
Functional Interfaces Examples
Runnable It represents a task that can be executed concurrently.
@FunctionalInterface
public interface Runnable {
void run();
}
Comparator It is used for comparing two objects of a specified type.
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
// Other default and static methods
}
Supplier It provides a single value of a specified type.
@FunctionalInterface
public interface Supplier<T> {
T get();
// Other default and static methods
}
Function It represents a function that accepts one argument and produces a result.
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
// Other default and static methods
}
Consumer It represents an operation that accepts a single input argument and returns no result.
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
// Other default and static methods
}
Example-
Usage of the lambda expression with the functional interface.
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
// Example 1: Function as a lambda expression
Function<Integer, Integer> square = (Integer x) -> x * x;
System.out.println(square.apply(5)); // Output: 25
// Example 2: Runnable as a lambda expression
Runnable task = () -> System.out.println("Hello, Functional Interfaces!");
new Thread(task).start();
}
}
Output-
25
Hello, Functional Interfaces!
Benefits of Functional Interfaces
Concise code- Lambda expressions provide a concise way to implement methods defined through functional interfaces.
Readability- The code is made readable by focusing on what needs to be done rather than how. Compatibility with Functional Programming- Java support for functional interfaces facilitates a highly responsive programming style, which is useful for tasks such as iterating over collections, parallel processing, event handling, and more
The functional interface in Java provides for the use of lambda expressions to represent abbreviated anonymous functions. They are a starting point for effective implementation of programming concepts in Java.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Functional Interface in Java
A function interface is an interface with exactly one abstract method. Function interfaces are used extensively in lambda expressions and method statements to assign values in contexts that can be efficiently implemented. They form the basis for programming features introduced in Java 8 and later.
Characteristics of Functional Interfaces
Single Abstract Method (SAM) Function interfaces have exactly one abstract method that requires a lambda expression or method reference.
Default and static methods Functional interfaces can have default methods (methods with default implementations) and static methods (utility methods).
Annotation
@FunctionalInterfaceAlthough not mandatory, annotating an interface with@FunctionalInterfaceclearly indicates that it is intended to be used as a functional interface If the interface does not meet the requirements of a functional interface, the compiler issues an error they.Functional Interfaces Examples
Runnable It represents a task that can be executed concurrently.
Comparator It is used for comparing two objects of a specified type.
Supplier It provides a single value of a specified type.
Function It represents a function that accepts one argument and produces a result.
Consumer It represents an operation that accepts a single input argument and returns no result.
Example-
Usage of the lambda expression with the functional interface.
Output-
Benefits of Functional Interfaces
Concise code- Lambda expressions provide a concise way to implement methods defined through functional interfaces.
Readability- The code is made readable by focusing on what needs to be done rather than how.
Compatibility with Functional Programming- Java support for functional interfaces facilitates a highly responsive programming style, which is useful for tasks such as iterating over collections, parallel processing, event handling, and more
The functional interface in Java provides for the use of lambda expressions to represent abbreviated anonymous functions. They are a starting point for effective implementation of programming concepts in Java.
Also, Read: What are lambda expressions in Java, and how are they used?