Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Streams and Functional

Functional interfaces in java.util.function

Overview of Java functional interfaces and the java.util.function package: Function, Predicate, Consumer, Supplier, operators, primitives and @FunctionalInterface.

A functional interface is an interface with exactly one abstract method. Lambdas and method references can be used wherever one is expected: the compiler turns the lambda into an implementation of that single method. Default and static methods do not count, nor do abstract methods that merely redeclare a public method of Object, such as equals.

The package java.util.function (since Java 8) provides about 43 ready-made functional interfaces so that you rarely need to write your own. Most of them are variations on four basic shapes: take a value and return another (Function), take a value and return a boolean (Predicate), take a value and return nothing (Consumer), and take nothing and return a value (Supplier).

Java
Function<String, Integer> length = String::length;
Predicate<String> empty = String::isEmpty;
Consumer<String> printer = System.out::println;
Supplier<List<String>> maker = ArrayList::new;

Try it

Java The four basic shapes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Writing your own functional interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The main families

Interface Method Shape
Function<T,R> R apply(T) T to R
BiFunction<T,U,R> R apply(T,U) T, U to R
UnaryOperator<T> T apply(T) T to T
BinaryOperator<T> T apply(T,T) T, T to T
Predicate<T> boolean test(T) T to boolean
BiPredicate<T,U> boolean test(T,U) T, U to boolean
Consumer<T> void accept(T) T to nothing
BiConsumer<T,U> void accept(T,U) T, U to nothing
Supplier<T> T get() nothing to T
Runnable (java.lang) void run() nothing to nothing
Callable<V> (java.util.concurrent) V call() throws Exception nothing to V
Comparator<T> (java.util) int compare(T,T) T, T to int

There are also primitive specializations (IntFunction, ToIntFunction, IntPredicate, IntUnaryOperator, IntSupplier, BooleanSupplier, and their Long and Double counterparts) that avoid boxing; see primitive functional interfaces.

Usage notes

  • @FunctionalInterface is optional but recommended on your own interfaces. The compiler then reports an error if a second abstract method is added.
  • Prefer the standard ones. Use Function, Predicate and friends in APIs so callers can pass existing method references; invent your own only for a domain meaning (like Validator above) or for checked exceptions, which the standard interfaces cannot throw.
  • Type inference picks the target type from context. A lambda has no type of its own, so var f = x -> x; does not compile.
  • Variable capture: lambdas can read local variables only if they are final or effectively final.
  • Default methods give composition: Function.andThen/compose, Predicate.and/or/negate, Consumer.andThen, Comparator.thenComparing.
  • Generics and wildcards: parameters like Function<? super T, ? extends R> accept more lambdas; follow the producer-extends, consumer-super rule.