Types of Functional Interfaces in Java
There are different types of functional interfaces present in the java.util.function package. Let us understand a few of them:
1. Consumer
A Consumer is a functional interface that represents an operation that accepts an input, performs some action on it, but does not return a result in the Java language's java.util.function package. It accepts an argument and executes the operation. It is often used for side-effect activities like printing, logging, or object modification. Accept is the only abstract method that the Consumer interface defines. It helps to represent an operation that accepts a single input argument and returns no result.
Example
import java.util.function.Consumer;
public class Example {
public static void main(String[] args) {
// Define a Consumer to print a string
Consumer<String> printString = str -> System.out.println(str);
// Use the Consumer to print a message
printString.accept("Consumer Functional Interface");
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Consumer Functional Interface
2. Predicate
A Predicate is a functional interface that represents a boolean-valued function of an input in the Java java.util.function package. It is frequently used to put a condition to the test and see if it holds true or not. One abstract method named test is defined in the Predicate interface. It helps to represent a predicate or boolean-valued function of one argument. It is used to filter out the values from the collection in Java.
Example
import java.util.function.Predicate;
public class Example {
public static void main(String[] args) {
// Define a Predicate to check if a number is even
Predicate<Integer> isEven = num -> num % 2 == 0;
// Test the Predicate
boolean result = isEven.test(10);
System.out.println("Is 10 even? \n" + result); // Output: Is 10 even? true
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Is 10 even?
true
3. Function
A function that takes an input and produces an output is represented by a functional interface called a function in Java's java.util.function package. Data transformation or mapping from one type to another is frequently utilised. The apply abstract method, which accepts an argument and produces a result, is the only one that the Function interface defines.
Example
import java.util.function.Function;
public class Example {
public static void main(String[] args) {
// Define a Function to double a number
Function<Integer, Integer> doubleNumber = num -> num * 2;
// Use the Function to double a number
int result = doubleNumber.apply(10);
System.out.println("Double of 10 is: " + result);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Double of 10 is: 20
4. Supplier
A Supplier is a functional interface that represents a supplier of results in Java's java.util.function package; when called, it does not require any input and instead returns a result. It is frequently utilised when you need to create or compute values inefficiently.
Example
import java.util.function.Supplier;
public class Example {
public static void main(String[] args) {
// Define a Supplier to generate a random number
Supplier<Double> randomNumberSupplier = () -> Math.random();
// Use the Supplier to generate a random number
double randomVal = randomNumberSupplier.get();
System.out.println("Random Number: " + randomVal);
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Random Number: 0.8635943284489002
Built-in Java Functional Interfaces
There are four built-in functional interfaces in Java:
-
Runnable: The runnable interface in Java helps us to define a method called run(). This method doesn't takes any arguments and returns no value. It is used to represent a task that can be executed asynchronously.
-
Callable: The callable interface in Java helps us to define a method called call(). This method returns a value of a specific type and can throw an exception. It is used to represent a task that can be executed asynchronously and that returns a result.
-
Comparable: The comparable interface in Java helps us to define a method called compareTo(). This method takes an object of the same type as the implementing class. It returns an integer value indicating the ordering of the two objects. It is used to represent objects that have a natural ordering, such as numbers or strings.
-
ActionListener: The ActionListener interface in Java helps us to define an abstract method actionPerformed(ActionEvent e). This interface handles events triggered by actions such as button clicks, menu selections, and other user interactions.
Frequently Asked Questions
What is functional interface in Java?
A functional interface is a type of interface. It has only one abstract method. This interface is used to show a single functionality. We can use functional interfaces where methods are treated as values, such as lambda expressions.
What do you understand by a lambda expression?
A lambda expression is a feature of Java 8. It is the easiest way to declare a function along with its parameters. It is a short way in Java to express a function. This can work as a value, and later on, we can use this value in our code.
Is it possible to have more than one abstract method for a functional interface?
No, this is not possible in the case of a functional interface in Java. This interface can only have one abstract method. If it has more than one abstract method, then this will not be anymore a functional interface.
What are the ways to create a functional interface?
There are several ways to create a functional interface. We can create a functional interface by defining an interface with only one abstract method. We can also use the @FunctionalInterface annotation. This annotation helps us to indicate an interface as a functional interface.
Conclusion
In this article, we have discussed the functional interface in Java. We have also discussed examples by using this interface. With the advent of lambda expressions in Java 8, functional interfaces have become increasingly important in Java development. They give programmers an easy method to treat functions as first-class citizens, allowing them to create clearer and more expressive code.
You can check out our other blogs to enhance your knowledge:
Happy Learning!!