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.

@FunctionalInterface public interface Consumer2<A,B>
A functional interface representing an operation that accepts two arguments and returns no result.

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:

// Lambda expression Consumer2<String,Integer> printer = (name, age) -> System.out.println(name + " is " + age + " years old"); printer.apply("Alice", 30); // Prints "Alice is 30 years old" // Method reference Map<String,Integer> map = new HashMap<>(); Consumer2<String,Integer> putter = map::put; putter.apply("key", 42); // Operation composition Consumer2<String,Integer> log = (s, i) -> logger.info(s + ": " + i); Consumer2<String,Integer> store = (s, i) -> cache.put(s, i); Consumer2<String,Integer> logAndStore = log.andThen(store); logAndStore.apply("data", 100); // Logs and stores

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default Consumer2<A,B>
    andThen(Consumer2<? super A,? super B> after)
    Returns a composed Consumer2 that performs, in sequence, this operation followed by the after operation.
    void
    apply(A a, B b)
    Performs this operation on the given arguments.
  • Method Details

    • andThen

      default Consumer2<A,B> andThen(Consumer2<? super A,? super B> after)
      Returns a composed Consumer2 that performs, in sequence, this operation followed by the after operation.

      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 after operation 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 be null.
      Returns:
      A composed Consumer2 that performs in sequence this operation followed by the after operation.
      Throws:
      NullPointerException - if after is null.
    • apply

      void apply(A a, B b)
      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.