☕ Java Intermediate

What is a functional interface in Java?

Answer

A functional interface is an interface that has exactly one abstract method (SAM — Single Abstract Method). They are the target types for lambda expressions and method references. The @FunctionalInterface annotation is optional but recommended — it causes a compile error if the interface has more than one abstract method. Java 8 built-in functional interfaces in java.util.function: Predicate<T> (takes T, returns boolean), Function<T,R> (takes T, returns R), Consumer<T> (takes T, returns void), Supplier<T> (takes nothing, returns T), BiFunction<T,U,R>, UnaryOperator<T>. Existing interfaces like Runnable and Comparator are also functional interfaces.