Interface Consumer4<A,B,C,D>

Type Parameters:
A - The type of the first argument to the operation.
B - The type of the second argument to the operation.
C - The type of the third argument to the operation.
D - The type of the fourth argument to the operation.
All Known Subinterfaces:
BeanPropertyConsumer, ThrowingConsumer4<A,B,C,D>
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 Consumer4<A,B,C,D>
A functional interface representing an operation that accepts four arguments and returns no result.

This interface extends the standard Java Consumer pattern to support four-argument consumers. It's useful when you need to pass operations with four 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(Consumer4) for operation chaining
  • Side-effect operations - designed for operations that perform side effects rather than return values
Use Cases:
  • Four-argument operations in iteration patterns
  • Builder methods that accept four-parameter operations
  • Callback patterns with four parameters
  • Event handlers that process four values
Usage:

// Lambda expression Consumer4<String,Integer,Boolean,Double> printer = (name, age, active, score) -> System.out.println(name + ": age=" + age + ", active=" + active + ", score=" + score); printer.apply("Alice", 30, true, 95.5); // Prints "Alice: age=30, active=true, score=95.5" // Operation composition Consumer4<String,Integer,Boolean,Double> validate = (s, i, b, d) -> { if (i < 0 || d < 0) throw new IllegalArgumentException(); }; Consumer4<String,Integer,Boolean,Double> process = (s, i, b, d) -> process(s, i, b, d); Consumer4<String,Integer,Boolean,Double> validateAndProcess = validate.andThen(process); validateAndProcess.apply("data", 42, true, 10.5); // Validates then processes

See Also:
  • Method Summary

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

    • andThen

      default Consumer4<A,B,C,D> andThen(Consumer4<? super A,? super B,? super C,? super D> after)
      Returns a composed Consumer4 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:

      Consumer4<String,Integer,Boolean,Double> log = (s, i, b, d) -> logger.info(s + ": " + i + ", " + b + ", " + d); Consumer4<String,Integer,Boolean,Double> store = (s, i, b, d) -> cache.put(s, i, b, d); Consumer4<String,Integer,Boolean,Double> logAndStore = log.andThen(store); logAndStore.apply("data", 100, true, 50.0); // Logs and stores

      Parameters:
      after - The operation to perform after this operation. Must not be null.
      Returns:
      A composed Consumer4 that performs in sequence this operation followed by the after operation.
      Throws:
      NullPointerException - if after is null.
    • apply

      void apply(A a, B b, C c, D d)
      Performs this operation on the given arguments.
      Example:

      Consumer4<String,Integer,Boolean,Double> handler = (key, value, flag, weight) -> { map.put(key, value); if (flag) cache.invalidate(key); stats.record(weight); }; handler.apply("user", 12345, true, 1.5);

      Parameters:
      a - The first operation argument.
      b - The second operation argument.
      c - The third operation argument.
      d - The fourth operation argument.