
DC Studio / Shutterstock.com
The functional interface in Java is a useful tool that can be used to promote cleaner programming practices and make it easier to build upon existing code bases.
In today s guide, we will go over what functional interfaces are, the functional interfaces built into Java, and how they can be used to their fullest. Let s dive right in!
What is a Functional Interface?
Put simply, a functional interface is an interface with a single abstract method. They are usually created for a single purpose, and they make it easier to integrate lambda expressions intoJava.
Here s an example of a functional interface:
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
The purpose ofCalculatoris to enable a programmer to make a quick lambda expression that can be used to make a given calculation.
As can be seen, there is only one method,calculate, and it accepts two arguments for an arbitrary mathematical expression. Note that the@FunctionalInterfaceannotation is not necessary, but it ensures that you are creating a proper functional interface. If we wanted to actually use the interface, we could do something like this:
Calculator add = (a, b) -> a + b;
System.out.println(add.calculate(2, 3)); // Output: 5
Here,Calculatoris used as a data type, and a lambda expression is assigned to it.

History-Computer.com
What is a Lambda Expression?
Toreallyunderstand the functional interface in Java, you need to know what a lambda expression is. A lambda expression is the shorthand version of any function that can be made to quickly perform a specific task. The general form of a lambda expression is:
(parameter1, parameter2, ...) -> {
// Body of the lambda expression
// This can be a single statement or a block of code
}
Lambda expressions can be assigned to a variable that is declared as a functional interface, and this allows for their generic functionality.
Types of Built-In Functional Interface in Java
There are many functional interfaces that are built into Java, and you may have used some of them before. Some of them utilize generics, as designated by<T>or<T, R>.
Predicate<T>
Predicates take in an argument of type T and return a boolean value. For example, if we wanted to check if a number is even, we could write:
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4)); // Output: true
System.out.println(isEven.test(7)); // Output: false
Function<T, R>
As can be assumed by its name, Function is used to represent functions. They take in an argument of type T and return a value of type R. If we wanted to take in a string and return its length, we could write:
Function<String, Integer> stringLength = str -> str.length();
System.out.println(stringLength.apply("Hello")); // Output: 5
Consumer<T>
A Consumer represents an operation performed with a given argument. It takes in an argument of type T and performs some action on it. For example, it can be used to print out every element of a list:
Consumer<String> printElement = str -> System.out.println(str);
List<String> list = Arrays.asList("Apple", "Banana", "Orange");
list.forEach(printElement);
Supplier<T>
A Supplier does not take any arguments, but it produces a value of type T. This can be useful in certain scenarios, such as if we want to generate a random number. Here s how we can create a Supplier that does this:
Supplier<Double> randomSupplier = () -> Math.random();
System.out.println(randomSupplier.get());
Functional Interface in Java: Wrapping Up
Although they may not always be necessary for your programs, if used correctly, functional interfaces can help you create more readable and maintainable code.
Java is not a functional language (rather, it is considered anobject-oriented language), but this tool is invaluable all the while. There is much more to know about functional interfaces, and we encourage you to learn as much as you can on yourprogrammingjourney!