Java functional interfaces overview¶
In the context of Java 8, functional interfaces serve as the foundation for Lambda expressions, analogous to callback functions in languages like JavaScript^[java8-lambda.md]. They allow methods to be passed as arguments, enabling the treatment of actions or logic as data^[java8-lambda.md].
Common Functional Interfaces¶
The java.util.function package provides several standard functional interfaces to define common lambda signatures.
Function¶
Function<T, R> represents a unary function that accepts one argument (of type T) and produces a result (of type R)^[java8-lambda.md]. It executes logic via the apply method^[java8-lambda.md#L21-22]. For example, it can be used to define operations that modify or transform an integer value^[java8-lambda.md#L24-26].
Consumer¶
Consumer<T> represents a unary function from T to void^[java8-lambda.md]. It is an operation that accepts a single input argument and returns no result.
Predicate¶
Predicate<T> represents a unary function from T to boolean^[java8-lambda.md]. It is typically used to test a condition and return a boolean result.
Supplier¶
Supplier<T> represents a nilary function (a function with no arguments) that returns a result of type R^[java8-lambda.md]. It is often used to generate or supply values without taking any input.
Sources¶
^[java8-lambda.md]