Java Functional Interfaces

Last Updated : 30 Nov, 2026

A functional interface in Java is an interface with exactly one abstract method. It can be implemented using lambda expressions or method references, making it useful for functional programming.

  • Contain multiple default and static methods.
  • It can extend another interface if the resulting interface has only one abstract method.
Java
public class Geeks {
    public static void main(String[] args){

        // Pass a lambda expression to Thread
        new Thread(
            () -> System.out.println("New thread created"))
            .start();
    }
}

Output
New thread created

Explanation:

  • Runnable has one abstract method, run().
  • The lambda expression provides the implementation of run().
  • The lambda is passed to the Thread constructor, which accepts a Runnable.
  • Calling start() starts a new thread that executes the lambda body.

Syntax of Functional Interface

@FunctionalInterface
interface InterfaceName {
returnType methodName(parameters);
}

The @FunctionalInterface annotation is optional. It instructs the compiler to verify that the interface satisfies the functional interface requirements.

Rules of Functional Interfaces

  • A functional interface must have exactly one abstract method, excluding methods matching public methods of Object.
  • It can contain multiple default and static methods.
  • It can inherit methods from other interfaces if they do not introduce additional independent abstract methods.
  • The @FunctionalInterface annotation enables compile-time validation but is not required.

Example: Using @FunctionalInterface Annotation

Program to creates a functional interface to calculate the square of a number.

Java
@FunctionalInterface
interface Square{
    int calculate(int x);
}

class Geeks{
    public static void main(String[] args){

        // Implement the interface using a lambda
        Square square = x -> x * x;

        System.out.println(square.calculate(5));
    }
}

Output
25

Explanation:

  • Square declares one abstract method, calculate(int x).
  • The lambda expression implements this method.
  • calculate(5) returns 25.

Functional Interfaces Before Java 8

Functional interfaces existed before Java 8. However, developers commonly used anonymous inner classes to implement them. Java 8 introduced lambda expressions and method references, providing shorter alternatives.

Example: Using an Anonymous Inner Class

Java
class Geeks{
    public static void main(String[] args){

        // Implement Runnable using an anonymous class
        new Thread(new Runnable(){
            @Override
            public void run(){
                System.out.println("New thread created");
            }
        }).start();
    }
}

Output
New thread created

The anonymous class implements run() explicitly. A lambda expression provides a shorter implementation for the same functional interface.

Types of Functional Interfaces

Java 8 introduced the java.util.function package with standard functional interfaces for common operations.

Functional Interface

Description

Abstract Method

Consumer

Accepts a value and performs an action without returning a result.

void accept(T t)

Predicate

Tests a condition and returns a boolean.

boolean test(T t)

Function

Converts an input value into a result.

R apply(T t)

Supplier

Provides a value without accepting an argument.

T get()

functional_interfaces
Functional Interfaces

Example: Using Predicate to Filter Strings

The following program uses the Predicate interface to check whether strings begin with G.

Java
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

class Geeks{
    public static void main(String[] args){

        List<String> names = Arrays.asList(
            "Geek", "GeeksQuiz", "g1", "QA", "Geek2"
        );

        // Check strings starting with G
        Predicate<String> predicate = s -> s.startsWith("G");

        for (String name : names){
            if (predicate.test(name)){
                System.out.println(name);
            }
        }
    }
}

Output
Geek
GeeksQuiz
Geek2

Explanation:

  • Predicate<String> defines a condition for string values.
  • The lambda expression checks whether a string starts with G.
  • test() evaluates each string and returns a boolean.
  • Matching strings are displayed.

Other Common Functional Interfaces

Apart from the four interfaces discussed above, Java provides several other functional interfaces.

Interface

Abstract Method

Runnable

void run()

Callable

V call() throws Exception

Comparable

int compareTo(T o)

BiConsumer

void accept(T t, U u)

BiPredicate

boolean test(T t, U u)

BiFunction

R apply(T t, U u)

UnaryOperator

T apply(T t)

BinaryOperator

T apply(T t1, T t2)

Comment