Consumer interface¶
The Consumer interface is a Functional Interface introduced in Java 8 (specifically within the java.util.function package) designed to represent an operation that accepts a single input argument and returns no result.^[600-developer__java__java8__java8-lambda.md]
Unlike other functional interfaces that produce output (such as Function<T, R>, which returns a value, or Predicate<T>, which returns a boolean), the Consumer executes a side-effect based on the input.^[600-developer__java__java8__java8-lambda.md] This concept is analogous to a callback function in JavaScript, where the goal is often to pass an "action" or "method" to be executed rather than to calculate a return value.^[600-developer__java__java8__java8-lambda.md]
Functional Signature¶
Formally, Consumer defines a functional method that acts as a unary function from T to void.^[600-developer__java__java8__java8-lambda.md] This means it takes one generic argument (T) and performs an operation without returning anything.
Related Interfaces¶
The Consumer interface is part of a set of standard functional interfaces provided in Java 8, each serving a specific purpose:
- Function
: Represents a function that accepts one argument and produces a result (T to R).^[600-developer__java__java8__java8-lambda.md] - Predicate: Represents a predicate (boolean-valued function) of one argument (T to boolean).^[600-developer__java__java8__java8-lambda.md]
- Supplier: Represents a supplier of results (function with no arguments returning R).^[600-developer__java__java8__java8-lambda.md]
[[Java Lambda]]¶
Functional Interface¶
Sources¶
^[600-developer__java__java8__java8-lambda.md]