Functional Interfaces in java
In the Java programming language, a functional interface is a user-defined function interface method that creates a single user-defined program abstract method. It is primarily used to represent lambda expression conditions and user-defined lambda method references in Java programs. This makes it possible to create more compact and easier-to-understand program source code within the functional programming modular paradigm in Java.

Important Features of a Functional Interface in Java
- Only one abstract method – A Java lambda expression defines only one abstract method in a functional interface. As such, this expression can contain multiple default or static functional methods.
- Can be used with lambda expressions – As in Java programs, a lambda expression connects to a single method in a functional interface. For this reason, such functional interfaces are often used as targets for lambda expressions.
- @FunctionalInterface Annotation – While this is not mandatory for functional interfaces, Java users can use the @FunctionalInterface annotation to indicate this in their program. Defining multiple annotations helps catch program mistakes if a Java user accidentally adds more than one abstract method to a lambda-like function.
Basic syntax of a Java functional interface.
@FunctionalInterface in lambda expression
public interface SampleFunctionalInterface {
void startAction(); // One user-defined abstract method define
// Optional: Default or static methods define a functional method
default void logAction() {
System.out.println(“Action started.”);
}
static void printMessage() {
System.out.println(“Static functional message displayed.”);
}
}
Features of a Java functional interface.
- Here, in this program, a functional interface must declare one and only one abstract user-defined method.
- Java users can create as many default and static methods as needed within a lambda expression functional interface.
- If a Java user-defined functional interface defines multiple abstract methods, it will no longer be treated as a functional interface in the Java program and will not work with existing lambda expressions.
Features of Commonly Used Functional Interfaces in the Java Language.
Java provides its programmers with several built-in library functional interfaces in the java.util.function package. These are commonly used in Java to perform functional programming tasks with lambda expressions.
Runnable Java Functional Interface.
A runnable functional interface in a Java program represents a task that can be executed asynchronously within the current program. The Runnable functional interface is defined by a single method, run(), that accepts no user input parameters from a program and returns no value.
Runnable task = () -> System.out.println(“User-defined Task is running now…”);
new Thread(task).start(); // Here it executes the Runnable task in a separate thread.
Single Abstract Method: void run()
Consumer<T> Java Functional Interface.
In a Java program, the Consumer<T> interface represents an operation that takes a single input argument value from the program and does not provide any output. This interface is commonly used in Java when the Java user wants to apply an action to a program object.
Consumer<String> info = message -> System.out.println(message);
info.accept(“Welcome to, Vcanhelpsu”); // here it results – Welcome to, Vcanhelpsu
Single Abstract Method: void accept(T t)
Supplier<T> Java Functional Interface.
In a Java program, the Supplier<T> functional interface represents the supplier of a result. The Supplier<T> functional interface contains a method, get(), that returns a value of type T. It does not take any input from the program.
Supplier<Double> randomSupplier = () -> Math.random();
System.out.println(randomSupplier.get()); // here it results, a random double value according to the program.
Single Abstract Method: T get()
Function<T, R> Java Functional Interface.
In a Java program, the Function<T, R> interface represents a function that takes an argument of type T as input and returns a result of type R. The Function<T, R> interface is commonly used to map or modify data.
Function<String, Integer> stringLength = str -> str.length();
System.out.println(stringLength.apply(“Vcanhelpsu”)); // here it results – 10
Single Abstract Method: R apply(T t)
Predicate<T> Java Functional Interface.
In Java programs, the Predicate<T> interface represents a function taking a single argument and returning a Boolean value. The Predicate<T> interface is commonly used to check whether an object in the current program satisfies a specific condition.
Predicate<Integer> isEven = integer -> integer % 2 == 0;
System.out.println(isEven.test(18)); // here it results – true
Single Abstract Method: boolean test(T t)
UnaryOperator<T> Java Functional Interface.
In Java programs, the UnaryOperator<T> interface is a special use case of Function<T, T>, which applies a transformation to user input and returns a result of the same type, T.
UnaryOperator<Integer> square_value = integer -> integer * integer;
System.out.println(square_value.apply(7)); // Here it is, result – 49
Single Abstract Method: T apply(T t)
BinaryOperator<T> Java Functional Interface.
The BinaryOperator<T> interface, BiFunction<T, T, T>, also has its own special use case format in Java programs. It accepts two arguments of the same type as input from the user and returns a result of the same type.
BinaryOperator<Integer> total = (p, q) -> p + q;
System.out.println(total.apply(3, 4)); // Here it is, result – 7
Single Abstract Method: T apply(T t1, T t2)
BiFunction<T, U, R> Java Functional Interface.
The BiFunction<T, U, R> interface in Java represents a function that takes two arguments of different types (T and U) as input and returns a result of type R.
BiFunction<Integer, Integer, Integer> mul = (p, q) -> p * q;
System.out.println(mul.apply(4, 8)); // here it results – 32
Single Abstract Method: R apply(T t, U u)
BiPredicate<T, U> Java Functional Interface.
The BiPredicate<T, U> interface in Java is similar to Predicate<T>, but it takes two argument values as input and returns a Boolean value as output.
BiPredicate<String, Integer> checkLength = (str, len) -> str.length() > len;
System.out.println(checkLength.test(“Vcanhelpsu”, 4)); // Here it results – true
Single Abstract Method: boolean test(T t, U u)
Example of Functional Interface Use in Java.
So, here we use such a function interface to convert a string to its length and then apply a condition using a Predicate.
import java.util.function.Function;
import java.util.function.Predicate;
public class FunctionalInterfaceIllustration {
public static void main(String[] args) {
/ Here we use a function to get the length of the given string
Function<String, Integer> stringLength = str -> str.length();
// here we use Predicate to check if length f string is greater than 7 or not
Predicate<Integer> isLengthGreaterThanSeven = length -> length > 7;
// here we Apply a function to get the length of the given string
String input = “Vcanhelpsu”;
Integer length = stringLength.apply(input);
// here we Apply predicate to check the above condition is true or not
if (isLengthGreaterThanSeven.test(length)) {
System.out.println(“here The given string is more than 7 characters.”);
} else {
System.out.println(“here is the The given string is 7 or less characters.”);
}
}
}
Advantages of using functional interfaces in the Java language.
- Simplifies code – By using lambda expressions in Java programs, Java programmers can create program code in a clearer and smaller format using functional interfaces.
- Promotes functional programming – Java functional interface programming can now take advantage of functional programming concepts such as immutability and declarative constructs.
- Improved reusability – Functional interfaces in Java, especially the java.util.function package library, allow many user-defined function methods and classes to be used multiple times as needed.
- Works with streams – The Java Functional Interface Program Streams API is the backbone of many operations, allowing Java programmers to perform programmatic operations such as element filtering, mapping, and reducing.
