Functional Interfaces in Java?

If you are already working on Java 8, then might know what is Functional Interface and may skip reading this blog. If not or if you want revise the basics of them, then continue reading it!
What is Functional Interface?
A functional interface in Java is an interface that specifies exactly one abstract method. You already know the abstract method is a method declaration without implementation.
Why we need it?
The functional interfaces are used as the basis for lambda expressions and method references in Java. If are not familiar with the lambda expressions and method references then it takes another post to explain in detail. So if you are not aware, to keep it short , you can think of lambda expression as anonymous function(a function without a name).
Let's continue…
Functional interfaces are needed because they provide a way for developers to represent functions as objects, which can be passed as arguments, returned from methods, or stored in variables. This makes functional interfaces a key component of functional programming in Java.
There are several functional interfaces available in the Java Standard Library, including
java.util.function.Consumer<T>- Represents an operation that accepts a single input argument and returns no result.java.util.function.Supplier<T>- Represents a supplier of results.java.util.function.Function<T, R>- Represents a function that takes an argument of typeTand returns a result of typeR.java.util.function.Predicate<T>- Represents a predicate (a boolean-valued function of one argument).java.util.function.BinaryOperator<T>- Represents an operation upon two operands of the same type, producing a result of the same type as the operands.java.util.function.UnaryOperator<T>- Represents an operation on a single operand that produces a result of the same type as its operand.
Lets see the code example for first four interfaces in this list as they most used interfaces in our day to day development.
java.util.function.Consumer<T>
Consumer<Integer> consumer = (x) -> System.out.println(x);
consumer.accept(10); // Output: 10In this example, Consumer<Integer> is a functional interface that takes an integer as an input argument and doesn't return any result. The lambda expression (x) -> System.out.println(x) implements the accept method of the Consumer interface and takes an integer as input. When the accept method is called with the value 10, it outputs 10.
2. java.util.function.Supplier<T>
Supplier<String> supplier = () -> "Hello World!";
System.out.println(supplier.get()); // Output: Hello World!In this example, Supplier<String> is a functional interface that doesn't take any input argument and returns a string. The lambda expression () -> "Hello World!" implements the get method of the Supplier interface and returns a string. When the get method is called, it returns the string "Hello World!".
3. java.util.function.Function<T, R>:
Function<Integer, String> function = (x) -> "Result: " + x;
System.out.println(function.apply(10)); // Output: Result: 10In this example, Function<Integer, String> is a functional interface that takes an integer as input and returns a string. The lambda expression (x) -> "Result: " + x implements the apply method of the Function interface and takes an integer as input and returns a string. When the apply method is called with the value 10, it returns the string "Result: 10".
Can I create my own Functional Interface?
yes, you can also create your own functional interface by defining an interface with a single abstract method. For example
@FunctionalInterface
public interface MyFunctionalInterface {
void doSomething(int x);
}The @FunctionalInterface annotation is optional but it's a good practice to include it to indicate that the interface is intended to be a functional interface and to catch accidental attempts to add additional abstract methods.
Once you have defined your functional interface, you can implement it using a lambda expression or by providing an implementation class. For example
// Using a lambda expression
MyFunctionalInterface myFunction = (x) -> System.out.println("Input: " + x);
myFunction.doSomething(10); // Output: Input: 10
// Using an implementation class
class MyFunctionImpl implements MyFunctionalInterface {
@Override
public void doSomething(int x) {
System.out.println("Input: " + x);
}
}
MyFunctionalInterface myFunction2 = new MyFunctionImpl();
myFunction2.doSomething(20); // Output: Input: 20In this example, we have defined a functional interface MyFunctionalInterface with a single abstract method doSomething that takes an integer argument x. We have implemented this interface using a lambda expression and an implementation class. When we call the doSomething method with the value 10 and 20 respectively, it outputs Input: 10 and Input: 20.
There are many more functional interfaces available in Java, these are just a few examples. These functional interfaces are part of the java.util.function package and provide a convenient way to write lambda expressions and method references in Java!
Hope you got some idea about the functional interfaces. Looking forward for your follow up for more blog posts!
Thanks for reading.





