Karol Stasiak's Blog

Coding from an elevator.

Java 8 Functional Interfaces Cheatsheet

| Comments

Java 8 introduced basic support for first-class functions. The functions, unlike in other languages, aren’t represented by only handful of types. Instead, Java 8 uses dozens of various types depending on arity, parameter types and the return type. The standard documentation is pretty unwieldy, so for my and your convenience, I prepared a list of all functional interfaces in a more useful order.

Few things to have in mind:

  • All interfaces are in java.util.function package unless otherwise noted.

  • Interfaces are specialized only for boolean, int, long and double, and only sometimes. Other primitive types, and all primitive types in certain situations, will have to be boxed.

Nullary functions

function type Java type
() → void java.lang.Runnable
() → boolean BooleanSupplier
() → int IntSupplier
() → long LongSupplier
() → double DoubleSupplier
() → A Supplier<A> or java.lang.Callable<A>

Whether to use Supplier or Callable, it’s mostly a matter of taste and semantics. Supplier has a method called get, Callable has call. This suggests that functions with larger side-effects should be Callable, and functions with small side-effects (mostly lazily-initialized values) should be Suppliers. Of course, this is only a suggestion.


Unary functions

function type Java type
intvoid IntConsumer
longvoid LongConsumer
doublevoid DoubleConsumer
Avoid Consumer<A>
intboolean IntPredicate
longboolean LongPredicate
doubleboolean DoublePredicate
Aboolean Predicate<A>
intint IntUnaryOperator
longint LongToIntFunction
doubleint DoubleToIntFunction
Aint ToIntFunction<A>
intlong IntToLongFunction
longlong LongUnaryOperator
doublelong DoubleToLongFunction
Along ToLongFunction<A>
intdouble IntToDoubleFunction
longdouble LongToDoubleFunction
doubledouble DoubleUnaryOperator
Adouble ToDoubleFunction<A>
AA UnaryOperator<A>
AB Function<A, B>

Note the following:

  • The primitive type of the argument is attached directly, the primitive type of the result uses the prefix To.
  • If the result is boolean, then the function is called a predicate. It’ it’s void, then a consumer.
  • If the parameter type and the result type are the same, the function is called a unary operator.
  • There are no specialized interfaces from a primitive to a reference.

Binary functions

function type Java type
(A, int) → void ObjIntConsumer<A>
(A, long) → void ObjLongConsumer<A>
(A, double) → void ObjDoubleConsumer<A>
(A, B) → void BiConsumer<A, B>
(A, B) → boolean BiPredicate<A, B>
(int, int) → int IntBinaryOperator
(long, long) → long LongBinaryOperator
(double, double) → double DoubleBinaryOperator
(A, A) → A BinaryOperator<A>
(A, B) → int ToIntFunction<A, B>
(A, B) → long ToLongBiFunction<A, B>
(A, B) → double ToDoubleBiFunction<A, B>
(A, B) → C BiFunction<A, B, C>

Ternary functions and above

None.

Comments