Predicate<T>, Function<T, R>, Consumer<T> and Supplier<T> are the four core functional interfaces in the java.util.function package. A predicate tests a value and returns a boolean, a function transforms a value into another, a consumer accepts a value and returns nothing, and a supplier produces a value from no input. Each has a single abstract method, so you can implement it with a lambda or a method reference.
Together, the java util function interfaces cover most of what lambdas and streams need.
| Interface | Method | Input | Output | Typical use |
|---|---|---|---|---|
Predicate<T> | boolean test(T t) | one value | boolean | filtering, validation |
Function<T, R> | R apply(T t) | one value | a result | mapping, conversion |
Consumer<T> | void accept(T t) | one value | nothing | side effects such as printing or saving |
Supplier<T> | T get() | nothing | a value | lazy creation, defaults, factories |
Each is annotated @FunctionalInterface. A functional interface has exactly one abstract method, and may have any number of default and static methods, which is where the combinators described below live.
A Predicate answers a yes or no question about a value.
and, or and negate build new predicates from existing ones, and Predicate.not(...) (Java 11) lets you negate a method reference: list.removeIf(Predicate.not(String::isBlank)). Streams use predicates in filter, anyMatch, allMatch and noneMatch; collections use them in removeIf.
A Function<T, R> maps an input of type T to a result of type R.
f.andThen(g) runs f first and then g; f.compose(g) runs g first and then f. Function.identity() returns its input and is common in Collectors.toMap. Streams use functions in map, flatMap and Collectors.groupingBy.
A Consumer takes a value and does something with it, so it is the interface behind forEach.
andThen chains two consumers that receive the same argument in order. Consumers exist to cause side effects, so keep them short and avoid hiding important logic in them.
A Supplier takes no input and returns a value each time you call get(). Because the work runs only when get is called, it supports lazy evaluation.
expensiveDefault is never called because the Optional has a value. With orElse(expensiveDefault()) it would run every time. Suppliers also appear in Stream.generate, Objects.requireNonNullElseGet, ThreadLocal.withInitial and Collectors.toCollection(TreeSet::new).
The package contains about forty interfaces. Most are variations of the four above.
BiPredicate<T, U>, BiFunction<T, U, R>, BiConsumer<T, U>.UnaryOperator<T> (a Function<T, T>) and BinaryOperator<T> (a BiFunction<T, T, T>).IntPredicate, IntFunction<R>, ToIntFunction<T>, IntConsumer, IntSupplier, BooleanSupplier, and the matching Long and Double versions.Prefer the primitive versions in hot code, such as IntStream.filter(IntPredicate), to avoid boxing costs.
Pick the interface from the shape of the operation: test gives Predicate, convert gives Function, act gives Consumer, create gives Supplier. Only define a custom functional interface when a name adds real meaning or you need a checked exception, since none of these interfaces allow checked exceptions in their methods.
Method references fit all four: String::isEmpty as a predicate, Integer::parseInt as a function, System.out::println as a consumer and ArrayList::new as a supplier.
A Function returns a result, while a Consumer returns void. Use Function to compute and Consumer to act.
Not directly, because the interface methods declare none. Catch the exception inside the lambda, or wrap it in an unchecked exception.
f.andThen(g) applies f then g. f.compose(g) applies g then f. Consumers only have andThen.
So the fallback is computed only when the Optional is empty, which avoids unnecessary work.
Predicate tests, Function transforms, Consumer acts and Supplier produces. Each has one abstract method and helper methods such as and, negate, andThen and compose. Learn the two argument, operator and primitive variants, and use method references to keep stream pipelines short.