Functional Interface in Java 8
Java 8 introduced a significant enhancement known as lambda expressions, which allowed for more concise and expressive code. Alongside lambda expressions, Java 8 also introduced a concept called “Functional Interface”. In this blog post, we will explore what is functional interface, provide examples of how to use them, and list some of the commonly used functional interfaces in Java 8.
1. What is a Functional Interface?
In Java, a functional interface is an interface that has only one abstract method. These interfaces are also referred to as “SAM” (Single Abstract Method) interfaces. The introduction of functional interfaces was pivotal in Java’s evolution as it enabled the use of lambda expressions to implement these interfaces concisely.
The key idea behind functional interfaces is to simplify the use of lambdas for method implementations, thus promoting clean and readable code. Java 8 provides a range of predefined functional interfaces for common use cases, making it easy to leverage lambda expressions in your code.
2. Using Functional Interface
To understand functional interfaces better, let’s consider a simple example where we create a functional interface and use it with a lambda expression:
package org.websparrow;
@FunctionalInterface
interface MyFunctionalInterface {
void doSomething(String message);
}
public class Test {
public static void main(String[] args) {
MyFunctionalInterface myLambda = (message) -> {
System.out.println("Doing something with the message: " + message);
};
myLambda.doSomething("Hello, Java 8!");
}
}
In this example, MyFunctionalInterface
is a functional interface with a single abstract method doSomething
. We create an instance of this functional interface using a lambda expression and call the doSomething
method.
Output:
Doing something with the message: Hello, Java 8!
Related Posts:
3. Commonly used Functional Interfaces in Java 8
Java 8 provides a set of commonly used functional interfaces in the java.util.function
package, which can simplify your coding efforts significantly. Here’s a list of some of these interfaces:
- Consumer<T>: Represents an operation that accepts a single argument and returns no result. It is used for side-effect operations.
- Supplier<T>: Represents a supplier of results. It has no arguments and provides a value.
- Predicate<T>: Represents a boolean-valued function that accepts one argument and returns a boolean result.
- Function<T, R>: Represents a function that accepts an argument of type
T
and produces a result of typeR
. - UnaryOperator<T>: Represents a function that accepts an argument of type
T
and produces a result of typeT
. - BinaryOperator<T>: Represents a function that accepts two arguments of type
T
and produces a result of typeT
.
These interfaces cover a wide range of use cases, making your code more modular and expressive. They are commonly used with lambda expressions to streamline various programming tasks.
4. Summary
Functional interfaces, alongside lambda expressions, are powerful additions to Java’s toolbox, allowing for cleaner and more expressive code. Additionally, Java 8’s predefined functional interfaces in the java.util.function
package simplify your development efforts, reducing the need for custom interface declarations.
5. References
- Package java.util.function – JavaDoc
- Overview of Java 8 Date and Time API
- Exploring the Optional in Java 8
- Lambda Expressions in Java 8