ProLesson 8.2Intermediate2 min readUpdated 30 October 2026
Functional interfaces
Meet Function, Predicate, Supplier, Consumer and their friends, combine them, and write your own with @FunctionalInterface.
A functional interface has exactly one abstract method, so a lambda can implement it. Java ships a set of ready-made ones in java.util.function, and four of them cover most needs.
01The big four
| Interface | Method | Takes → returns | Think of it as |
|---|---|---|---|
Function<T, R> | apply | T → R | Transform |
Predicate<T> | test | T → boolean | Check a condition |
Supplier<T> | get | nothing → T | Produce a value |
Consumer<T> | accept | T → nothing | Do something with a value |
BigFour.java
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class BigFour {
public static void main(String[] args) {
Function<String, Integer> length = s -> s.length();
Predicate<String> isEmail = s -> s.contains("@");
Supplier<String> newId = () -> "ID-" + 1001;
Consumer<String> log = msg -> System.out.println("[log] " + msg);
System.out.println(length.apply("Kafka"));
System.out.println(isEmail.test("asha@veltoryx.dev"));
System.out.println(newId.get());
log.accept("order placed");
}
}5 true ID-1001 [log] order placed
Loading the rest of the lesson…