Built-in functional interfaces (java.util.function)
◈ 4 cardsThe six core interfaces, their method names, and when to reach for each.
The standard toolkit
java.util.function ships a family of ready-made functional interfaces so you don't have to invent your own. The six you must know for OCP:
| Interface | Signature | Use |
|---|---|---|
Supplier<T> | T get() | produce a T from nothing |
Consumer<T> | void accept(T t) | consume a T, return nothing |
Function<T, R> | R apply(T t) | map T to R |
Predicate<T> | boolean test(T t) | test T, return a boolean |
UnaryOperator<T> | T apply(T t) | specialised Function where T == R |
BinaryOperator<T> | T apply(T t1, T t2) | specialised BiFunction where all types match |
BiFunction<T, U, R> is the two-input generalisation of Function: R apply(T t, U u).
Primitive specialisations avoid boxing overhead — IntFunction<R>, IntConsumer, IntPredicate, IntSupplier, ToIntFunction<T>, IntUnaryOperator, and so on for Long and Double. Prefer them in hot paths.
Supplier<String> greet = () -> "hello";
Consumer<String> printer = System.out::println;
Function<String, Integer> len = String::length;
Predicate<String> blank = String::isBlank;
UnaryOperator<String> upper = String::toUpperCase;
BinaryOperator<Integer> max = Integer::max;