Functional Interfaces
Functional interfaces are also known as Single Abstract Method interfaces (SAM Interfaces). As its name suggests, a functional interface allows only one abstract method in it.
Java 8 has introduced @FunctionalInterface annotation to mark an interface as functional interface. This annotation should be used to give compile-time error if a functional interface violates the contracts.
Example
@FunctionalInterface
public interface FunctionalInterfaceExample {
public void display();
}
java.lang.Runnable is also a funtional interface since it has just one abstract method. Below is a list of predefined functional interfaces.
Rules for functional interfaces
- A functional interface can have only one abstract method. But it can have default and static methods.
- A functional interface cannot extend another functional interface.
- A functional interface can extend an interface which doesn’t have any abstract method i.e. It only has default and static methods.
Functional interface with default and static methods
@FunctionalInterface
public interface FunctionalInterfaceExample {
public void display();
//default method which has definition
default void displayDefaultMessage() {
System.out.println("This is default message.");
}
static void displayStaticMessage () {
System.out.println("This is static message of the inerface.");
}
}
Predefined functional interfaces
Interface | Description |
---|---|
BiConsumer<T, U> | It has a method that accepts two input arguments and returns no result. |
Consumer<T> | It has a method that accepts a single argument and returns no result. |
Function<T, R> | It has a method accepts one argument and returns a result. |
Predicate<T> | It has a predicate (boolean-valued function) of one argument. |
BiFunction<T,U,R> | It has a method that accepts two arguments and returns a a result. |
BinaryOperator<T> | It has a method upon two operands of the same data type. It returns a result of the same type as the operands. |
BiPredicate<T,U> | It has a predicate (boolean-valued function) of two arguments. |
BooleanSupplier | It represents a supplier of boolean-valued results. |
DoubleBinaryOperator | It represents an operation upon two double type operands and returns a double type value. |
DoubleConsumer | It has a method that accepts a single double type argument and returns no result. |
DoubleFunction<R> | It has a method that accepts a double type argument and produces a result. |
DoublePredicate | It has a predicate (boolean-valued function) of one double type argument. |
DoubleSupplier | It represents a supplier of double type results. |
DoubleToIntFunction | It has a method that accepts a double type argument and produces an int type result. |
DoubleToLongFunction | It has a method that accepts a double type argument and produces a long type result. |
DoubleUnaryOperator | It represents an operation on a single double type operand that produces a double type result. |
IntBinaryOperator | It represents an operation upon two int type operands and returns an int type result. |
IntConsumer | It has a method that accepts a single integer argument and returns no result. |
IntFunction<R> | It has a method that accepts an integer argument and returns a result. |
IntPredicate | It represents a predicate (boolean-valued function) of one integer argument. |
IntSupplier | It represents a supplier of integer type. |
IntToDoubleFunction | It represents a function that accepts an integer argument and returns a double. |
IntToLongFunction | It represents a function that accepts an integer argument and returns a long. |
IntUnaryOperator | It represents an operation on a single integer operand that produces an integer result. |
LongBinaryOperator | It represents an operation upon two long type operands and returns a long type result. |
LongConsumer | It represents an operation that accepts a single long type argument and returns no result. |
LongFunction<R> | It has a method that accepts a long type argument and returns a result. |
LongPredicate | It represents a predicate (boolean-valued function) of one long type argument. |
LongSupplier | It represents a supplier of long type results. |
LongToDoubleFunction | It has a method that accepts a long type argument and returns a result of double type. |
LongToIntFunction | It represents a function that accepts a long type argument and returns an integer result. |
LongUnaryOperator | It represents an operation on a single long type operand that returns a long type result. |
ObjDoubleConsumer<T> | It has a method that accepts an object and a double argument, and returns no result. |
ObjIntConsumer<T> | It has a method that accepts an object and an integer argument. It does not return result. |
ObjLongConsumer<T> | It has a method that accepts an object and a long argument, it returns no result. |
Supplier<T> | It represents a supplier of results. |
ToDoubleBiFunction<T,U> | It has a method that accepts two arguments and produces a double type result. |
ToDoubleFunction<T> | It has a method that returns a double type result. |
ToIntBiFunction<T,U> | It has a method that accepts two arguments and returns an integer. |
ToIntFunction<T> | It has a method that returns an integer. |
ToLongBiFunction<T,U> | It has a method that accepts two arguments and returns a result of long type. |
ToLongFunction<T> | It has a method that returns a result of long type. |
UnaryOperator<T> | It represents an operation on a single operand that returnsa a result of the same type as its operand. |