Interface Consumer2<A,B>
- Type Parameters:
A- The type of the first argument to the operation.B- The type of the second argument to the operation.
- All Known Subinterfaces:
ThrowingConsumer2<A,B>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This interface extends the standard Java Consumer pattern to support
two-argument consumers. It's useful when you need to pass operations with two parameters to methods
that expect functional interfaces, such as in iteration, builders, or callback patterns.
Features:
- Functional interface - can be used with lambda expressions and method references
- Composition support - provides
andThen(Consumer2)for operation chaining - Side-effect operations - designed for operations that perform side effects rather than return values
Use Cases:
- Two-argument operations in iteration patterns
- Builder methods that accept two-parameter operations
- Callback patterns with two parameters
- Event handlers that process two values
Usage:
See Also:
Consumer3- Three-argument consumerConsumer4- Four-argument consumerConsumer5- Five-argument consumer- juneau-commons Basics
-
Method Summary
-
Method Details
-
andThen
Returns a composedConsumer2that performs, in sequence, this operation followed by theafteroperation.This method enables operation composition, allowing you to chain multiple operations together. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the
afteroperation will not be performed.Example:
Consumer2<String,Integer>
validate = (s ,i ) -> {if (i < 0)throw new IllegalArgumentException(); }; Consumer2<String,Integer>process = (s ,i ) ->process (s ,i ); Consumer2<String,Integer>validateAndProcess =validate .andThen(process );validateAndProcess .apply("data" , 42);// Validates then processes - Parameters:
after- The operation to perform after this operation. Must not benull .- Returns:
- A composed
Consumer2that performs in sequence this operation followed by theafteroperation. - Throws:
NullPointerException- ifafterisnull .
-
apply
Performs this operation on the given arguments.Example:
Consumer2<String,Integer>
handler = (key ,value ) -> {map .put(key ,value );cache .invalidate(key ); };handler .apply("user" , 12345);- Parameters:
a- The first operation argument.b- The second operation argument.
-