[Java] About the Java functional interface

1 minute read

What is a functional interface?

An interface that has only one abstract method, which can be the destination of a lambda expression or method reference.

Condition of functional interface

**・You can have only one abstract method

  • Abstract methods that are public methods of the Object class are not included. ・It is possible to have multiple default and static methods**

The definition of the functional interface is written as follows.


public interface Sample {
    public abstract void Hello(String name);

By annotating @FunctionalInterface, you can specify that it is a functional interface, and you can get a compile error when the interface does not meet the conditions of a functional interface.


Several types of functional interfaces are provided in the java.util.function package. There are roughly 4 types according to the argument received and the pattern of the result returned. The basic functional interface is:

| Types | Abstract methods | What you can do | |:—————–|——————:|:——— ———:| Supplier | T get() | has no arguments and returns a return value of type T | | Consumer | void accept(T t) | Returns the process (no value is returned). | |Function<T, R> | R apply(T t) | Receives T as an argument and returns R as a result | |Predicate |boolean test(T t)|Receives an argument of type T and returns the boolean value as a result.

There are other types such as interfaces with two arguments, so use them according to the application.