Archive

Java functional interfaces reference

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


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 methodInput→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 interfaceFunctional methodInput→Output
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 methodInput→Output
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 methodInput→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
DoubleToIntFunctionint applyAsInt(double value)double→int
DoubleToLongFunctionlong applyAsLong(double value)double→long
IntToDoubleFunctiondouble applyAsDouble(int value)int→double
IntToLongFunctionlong applyAsLong(int value)int→long
LongToDoubleFunctiondouble applyAsDouble(long value)long→double
LongToIntFunctionint applyAsInt(long value)long→int
UnaryOperator<T>T apply(T t)T→T
DoubleUnaryOperatordouble applyAsDouble(double operand)double→double
IntUnaryOperatorint applyAsInt(int operand)int→int
LongUnaryOperatorlong 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
DoubleBinaryOperatordouble applyAsDouble(double left, double right)double, double→double
IntBinaryOperatorint applyAsInt(int left, int right)int, int→int
LongBinaryOperatorlong 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 interfaceFunctional methodInput→Output
Predicate<T>boolean test(T t)T→boolean
BiPredicate<T,U>boolean test(T t, U u)T, U→boolean
DoublePredicateboolean test(double value)double→boolean
IntPredicateboolean test(int value)int→boolean
LongPredicateboolean 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 interfaceFunctional methodInput→Output
Runnablevoid run()→