Java functional interfaces reference

Java functional interfaces reference

in

This post is a quick reference on the built-in functional interfaces available in the Java API.

A functional interface is an interface that has exaclty one abstract method. It may also contain default and static methods which do have an implementation. Functional interfaces will most likely be annotated with @FunctionalInterface, indicating that the annotated interface in intented to be a functional interface, as defined above.

Functional interfaces were introduced in Java 8, are associated with lambda expressions, and are extensively used in the Stream API. So you certainly want to be familiar with the main built-in functional interfaces, along with what they take as input and produce as output:

Functional interface Functional method Input Output
Consumer<T> void accept(T t) T  
Supplier<T> T get()   T
Function<T,R> R apply(T t) T R
Predicate<T> boolean test(T t) T boolean

For variations on the input arguments and results, the following tables will come handy:

Consumers

A consumer represents an operation that accepts a single input argument and returns no result.

Functional interface Functional method Input Output
Consumer<T> void accept(T t) T  
BiConsumer<T,U> void accept(T t, U u) T, U  
DoubleConsumer void accept(double value) double  
IntConsumer void accept(int value) int  
LongConsumer void accept(long value) long  
ObjDoubleConsumer<T> void accept(T t, double value) T, double  
ObjIntConsumer<T> void accept(T t, int value) T, int  
ObjLongConsumer<T> void accept(T t, long value) T, long  

Suppliers

A supplier represents an operation that accepts no arguments and returns a result.

Functional interface Functional method Input Output
Supplier<T> T get()   T
BooleanSupplier boolean getAsBoolean()   boolean
DoubleSupplier double getAsDouble()   double
IntSupplier int getAsInt()   int
LongSupplier long getAsLong()   long

Functions

A function represents an operation that accepts argument(s) and produces a result. For operations meant to return boolean results, have a look at the predicates in the next section.

Functional interface Functional method Input Output
Function<T,R> R apply(T t) T R
DoubleFunction<R> R apply(double value) double R
IntFunction<R> R apply(int value) int R
LongFunction<R> R apply(long value) long R
ToDoubleFunction<T> double applyAsDouble(T value) T double
ToIntFunction<T> int applyAsInt(T value) T int
ToLongFunction<T> long applyAsLong(T value) T long
DoubleToIntFunction int applyAsInt(double value) double int
DoubleToLongFunction long applyAsLong(double value) double long
IntToDoubleFunction double applyAsDouble(int value) int double
IntToLongFunction long applyAsLong(int value) int long
LongToDoubleFunction double applyAsDouble(long value) long double
LongToIntFunction int applyAsInt(long value) long int
UnaryOperator<T> T apply(T t) T T
DoubleUnaryOperator double applyAsDouble(double operand) double double
IntUnaryOperator int applyAsInt(int operand) int int
LongUnaryOperator long applyAsLong(long operand) long long
BiFunction<T,U,R> R apply(T t, U u) T, U R
BinaryOperator<T> T apply(T t1, T t2) T, T T
ToDoubleBiFunction<T,U> double applyAsDouble(T t, U u) T, U double
ToIntBiFunction<T,U> int applyAsInt(T t, U u) T, U int
ToLongBiFunction<T,U> long applyAsLong(T t, U u) T, U long
DoubleBinaryOperator double applyAsDouble(double left, double right) double, double double
IntBinaryOperator int applyAsInt(int left, int right) int, int int
LongBinaryOperator long applyAsLong(long left, long right) long, long long

Predicates

A predicate (or boolean-valued function) represents an operation that accepts argument(s) and returns a boolean result.

Functional interface Functional method Input Output
Predicate<T> boolean test(T t) T boolean
BiPredicate<T,U> boolean test(T t, U u) T, U boolean
DoublePredicate boolean test(double value) double boolean
IntPredicate boolean test(int value) int boolean
LongPredicate boolean test(long value) long boolean

Runnable

Runnable was introduced back in JDK 1.0, and it’s indeed a functional interface, as it defines exactly one abstract method, which doesn’t take any arguments neither returns a value:

Functional interface Functional method Input Output
Runnable void run()