Java › Functional Interfaces & Method References
What is a functional interface, and what are the four core functional interfaces in java.util.function (Function, Predicate, Consumer, Supplier)?
BeginnerAnswer
A functional interface has exactly one abstract method, making it eligible for lambda expression or method reference assignment -- Function<T,R> transforms an input of type T into an output of type R (apply()); Predicate<T> tests a condition, returning boolean (test()); Consumer<T> accepts an input and performs a side effect, returning nothing (accept()); Supplier<T> takes no input and produces a value (get()) -- these four generic interfaces cover the vast majority of common functional programming needs without requiring a custom interface for each specific case.
Function<String, Integer> length = String::length;
Predicate<String> isEmpty = String::isEmpty;
Consumer<String> printer = System.out::println;
Supplier<String> greeting = () -> "Hello";
System.out.println(length.apply("hello")); // 5
System.out.println(isEmpty.test("")); // trueReal-world example A validation pipeline uses Predicate<Order> to check business rules, Function<Order, Invoice> to transform an order into an invoice, and Consumer<Invoice> to send the resulting invoice via email, composing a complete order-processing flow entirely out of these standard functional interfaces without defining any custom ones.