Interface Function4<A,B,C,D,R>

Type Parameters:
A - The type of the first argument to the function.
B - The type of the second argument to the function.
C - The type of the third argument to the function.
D - The type of the fourth argument to the function.
R - The type of the result of the function.
All Known Subinterfaces:
ThrowingFunction4<A,B,C,D,R>
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 Function4<A,B,C,D,R>
A functional interface representing a function that accepts four arguments and produces a result.

This interface extends the standard Java Function pattern to support four-argument functions. It's useful when you need to pass functions with four parameters to methods that expect functional interfaces, such as in stream operations, builders, or callback patterns.

Features:
  • Functional interface - can be used with lambda expressions and method references
  • Composition support - provides andThen(Function) for function chaining
  • Type-safe - generic type parameters ensure compile-time type safety
Use Cases:
  • Four-argument transformations in functional programming patterns
  • Builder methods that accept four-parameter functions
  • Stream operations requiring four-argument functions
  • Callback patterns with four parameters
Usage:

// Lambda expression Function4<String,Integer,Boolean,Double,String> format = (s, i, b, d) -> s + "-" + i + "-" + (b ? "Y" : "N") + "-" + d; String result = format.apply("prefix", 42, true, 95.5); // Returns "prefix-42-Y-95.5" // Function composition Function4<Integer,Integer,Integer,Integer,Integer> add = (a, b, c, d) -> a + b + c + d; Function4<Integer,Integer,Integer,Integer,String> addAndFormat = add.andThen(Object::toString); String sum = addAndFormat.apply(5, 3, 2, 1); // Returns "11"

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default <V> Function4<A,B,C,D,V>
    andThen(Function<? super R,? extends V> after)
    Returns a composed function that first applies this function to its input, and then applies the after function to the result.
    apply(A a, B b, C c, D d)
    Applies this function to the given arguments.
  • Method Details

    • andThen

      default <V> Function4<A,B,C,D,V> andThen(Function<? super R,? extends V> after)
      Returns a composed function that first applies this function to its input, and then applies the after function to the result.

      This method enables function composition, allowing you to chain multiple transformations together.

      Example:

      Function4<Integer,Integer,Integer,Integer,Integer> multiply = (a, b, c, d) -> a * b * c * d; Function4<Integer,Integer,Integer,Integer,String> multiplyAndFormat = multiply.andThen(n -> "Result: " + n); String result = multiplyAndFormat.apply(2, 3, 2, 2); // Returns "Result: 24"

      Type Parameters:
      V - The type of output of the after function, and of the composed function.
      Parameters:
      after - The function to apply after this function is applied. Must not be null.
      Returns:
      A composed function that first applies this function and then applies the after function.
      Throws:
      NullPointerException - if after is null.
    • apply

      R apply(A a, B b, C c, D d)
      Applies this function to the given arguments.
      Example:

      Function4<String,Integer,Boolean,Double,String> format = (s, n, upper, mult) -> { String result = s.repeat(n); result = upper ? result.toUpperCase() : result; return result + " x" + mult; }; String result = format.apply("ha", 2, true, 1.5); // Returns "HAHA x1.5"

      Parameters:
      a - The first function argument.
      b - The second function argument.
      c - The third function argument.
      d - The fourth function argument.
      Returns:
      The function result.