Java Functional Interfaces

Introduced in Java 8, a functional interface is simply an interface that has exactly one abstract method. Learn more about functional interfaces in this tutorial.

1. What are Functional Interfaces?

Functional interfaces are new additions in Java 8 and they contain exactly one abstract method. These functional interfaces are also called Single Abstract Method interfaces (SAM Interfaces).

Apart from one abstract method, a functional interface can also have the following methods that do not count for defining it as a functional interface.

In Java, functional interfaces can represent lambda expressions, method references and constructor references as well.

For example, check out the source code of the Comparator interface. It is a functional interface. Comparator has only two abstract methods compare() and equals(). But equals() has been inherited from Object class so it is not counted.

Other than the above two methods, all other methods are default methods.So Comparator is qualified to be declared as a functional interface.

@FunctionalInterface
public interface Comparator<T> {
	int compare(T o1, T o2);
	boolean equals(Object obj);

	//and multiple default methods...
}

2. @FunctionalInterface Annotation

Java 8 introduced the annotation @FunctionalInterface to mark an interface as a functional interface. The primary use of this annotation is for compiler-level errors when the interface violates the contracts of exactly one abstract method.

Note that if we do not use the annotation @FunctionalInterface on an interface with one abstract method, the interface is still a functional interface and it can be the target type for lambda expressions.

The presence of the annotation protects us from inadvertently changing a functional interface into a non-functional interface, as the compiler will catch it.

Let’s build our first functional interface:

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
    public void firstWork();
}

Let’s try to add another abstract method:

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
    public void firstWork();
    public void doSomeMoreWork();   //error
}

The above code will result in a compiler error:

Unexpected @FunctionalInterface annotation
@FunctionalInterface ^ MyFirstFunctionalInterface is not a functional interface
multiple non-overriding abstract methods found in interface MyFirstFunctionalInterface
Functional-Interface-Error

Read More : Generic Functional Interfaces

3. Commonly Used Functional Interfaces

The following is a list of the most commonly used functional interfaces in Java.

  • Predicate: a boolean-valued function that takes an argument and returns true or false.
  • BiPredicate: a predicate with two arguments.
  • Consumer: an operation that takes an argument, operates on it, and returns no result.
  • BiConsumer: a consumer with two arguments.
  • Supplier: a supplier that returns a value.
  • Function<T, R>:  takes an argument of type T and returns a result of type R.
  • BiFunction<T, U, R>: takes two arguments of types T and U and returns a result of type R.

4. Demo

Let’s see a quick example of how to create and use functional interfaces in Java.

We are using a functional interface Function to create the formula for mathematical square.

Function<Integer, Integer> square = x -> x * x;

The Function interface has one abstract method apply() that we have implemented above. we can execute the above method as follows:

System.out.println( square.apply(5) );  //Prints 25

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

15 thoughts on “Java Functional Interfaces”

  1. Thanks for the details
    # It will really help readers if we can add more scenarios when this particular feature can/should be used
    # Which current features it can replace or act as an alternative to
    # What would be the BIG-O or any performance metric we can back up with

    Reply
    • No A Functional Interface can not extends another Funtional Interface as the child funtional interface will be having two abstract method which will give exception.

      Reply
  2. I have doubt . What is the main purpose of implementing functional interfaces in java8 ?

    Thanks to you lokesh, now I know about functional interfaces but dont know when to use it ?

    Reply
  3. Nice one.This is absolutely clear to understand.
    But only one doubt in which scenario we will use Functional Interface/SAM Interface ?

    Thanks & Regards
    Tony

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.