Programming

Java functional interfaces reference

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 exactly 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 interfaceFunctional methodInputOutput
Consumer<T>void accept(T t)T
Supplier<T>T get()T
Function<T,R>R apply(T t)TR
Predicate<T>boolean test(T t)Tboolean

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 interfaceFunctional methodInputOutput
Consumer<T>void accept(T t)T
BiConsumer<T,U>void accept(T t, U u)T, U
DoubleConsumervoid accept(double value)double
IntConsumervoid accept(int value)int
LongConsumervoid 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 interfaceFunctional methodInputOutput
Supplier<T>T get()T
BooleanSupplierboolean getAsBoolean()boolean
DoubleSupplierdouble getAsDouble()double
IntSupplierint getAsInt()int
LongSupplierlong 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 interfaceFunctional methodInputOutput
Function<T,R>R apply(T t)TR
DoubleFunction<R>R apply(double value)doubleR
IntFunction<R>R apply(int value)intR
LongFunction<R>R apply(long value)longR
ToDoubleFunction<T>double applyAsDouble(T value)Tdouble
ToIntFunction<T>int applyAsInt(T value)Tint
ToLongFunction<T>long applyAsLong(T value)Tlong
DoubleToIntFunctionint applyAsInt(double value)doubleint
DoubleToLongFunctionlong applyAsLong(double value)doublelong
IntToDoubleFunctiondouble applyAsDouble(int value)intdouble
IntToLongFunctionlong applyAsLong(int value)intlong
LongToDoubleFunctiondouble applyAsDouble(long value)longdouble
LongToIntFunctionint applyAsInt(long value)longint
UnaryOperator<T>T apply(T t)TT
DoubleUnaryOperatordouble applyAsDouble(double operand)doubledouble
IntUnaryOperatorint applyAsInt(int operand)intint
LongUnaryOperatorlong applyAsLong(long operand)longlong
BiFunction<T,U,R>R apply(T t, U u)T, UR
BinaryOperator<T>T apply(T t1, T t2)T, TT
ToDoubleBiFunction<T,U>double applyAsDouble(T t, U u)T, Udouble
ToIntBiFunction<T,U>int applyAsInt(T t, U u)T, Uint
ToLongBiFunction<T,U>long applyAsLong(T t, U u)T, Ulong
DoubleBinaryOperatordouble applyAsDouble(double left, double right)double, doubledouble
IntBinaryOperatorint applyAsInt(int left, int right)int, intint
LongBinaryOperatorlong applyAsLong(long left, long right)long, longlong

Predicates #

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

Functional interfaceFunctional methodInputOutput
Predicate<T>boolean test(T t)Tboolean
BiPredicate<T,U>boolean test(T t, U u)T, Uboolean
DoublePredicateboolean test(double value)doubleboolean
IntPredicateboolean test(int value)intboolean
LongPredicateboolean test(long value)longboolean

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 interfaceFunctional methodInputOutput
Runnablevoid run()