Functional interfaces are a key concept in Java’s functional programming capabilities, especially introduced with Java 8. These interfaces have exactly one abstract method, and they can be implemented using lambda expressions, method references, or anonymous classes. Java’s standard library provides several built-in functional interfaces under the package java.util.function
.
Characteristics of Functional Interfaces
- Only one abstract method (can have default and static methods)
- Annotated with
@FunctionalInterface
(optional but recommended) - Enables cleaner code using lambdas or method references
Why Use Functional Interfaces?
- Encourages functional programming style
- Reduces boilerplate code
- Enables usage of Streams API,
Optional
, and parallel processing - Promotes cleaner and reusable logic
Functional Interfaces in Java Standard Library
Functional Interface | Method Signature | Purpose |
---|---|---|
Function<T, R> |
R apply(T t); |
Transforms a value of type T to type R |
BiFunction<T, U, R> |
R apply(T t, U u); |
Combines two values of types T and U into R |
Consumer<T> |
void accept(T t); |
Performs an action on a single input |
BiConsumer<T, U> |
void accept(T t, U u); |
Performs an action on two inputs |
Supplier<T> |
T get(); |
Supplies or generates a value without input |
Predicate<T> |
boolean test(T t); |
Evaluates a condition on a value |
BiPredicate<T, U> |
boolean test(T t, U u); |
Evaluates a condition on two values |
UnaryOperator<T> |
T apply(T t); |
Performs an operation on a value, returning same type |
BinaryOperator<T> |
T apply(T t, T u); |
Combines two values of the same type |
IntPredicate |
boolean test(int value); |
Evaluates a condition on an int value |
LongSupplier |
long getAsLong(); |
Supplies a long value |
DoubleFunction<R> |
R apply(double value); |
Maps a double input to a value of type R |
Functional interfaces are the foundation of functional programming in Java, introduced with Java 8. They enable the use of lambda expressions and method references, making code more concise, readable, and expressive. The Java Standard Library, particularly the java.util.function
package, provides a rich set of built-in functional interfaces such as Function
, Consumer
, Supplier
, and Predicate
, each tailored for a specific purpose — transforming data, consuming values, supplying values, and testing conditions respectively.
These interfaces simplify common programming tasks like data processing, event handling, stream operations, and functional pipelines. Their standardized method signatures and strong typing support lead to better maintainability and cleaner design in both object-oriented and functional-style code.