The java.util.function package in Java provides a set of functional interfaces that facilitate functional programming by enabling the use of lambda expressions and method references. Introduced in Java 8 as part of the java.util package, these interfaces play a crucial role in making Java a more functional language, particularly for operations on collections, streams, and other scenarios requiring functional-style programming.
The java.util.function package includes several key functional interfaces:
Predicate<T>
- Purpose: Represents a predicate (boolean-valued function) of one argument.
- Method: boolean test(T t)
Function<T, R>
- Purpose: Represents a function that accepts one argument and produces a result.
- Method: R apply(T t)
Consumer<T>
- Purpose: Represents an operation that accepts a single input argument and returns no result.
- Method: void accept(T t)
Supplier<T>
- Purpose: Represents a supplier of results, which produces a result of type T.
- Method: T get()
UnaryOperator<T>
- Purpose: Represents a function that takes a single argument of type T and returns a result of type T.
- Method: T apply(T t)
BinaryOperator<T>
- Purpose: Represents a function that takes two arguments of type T and returns a result of type T.
- Method: T apply(T t1, T t2)
BiFunction<T, U, R>
- Purpose: Represents a function that accepts two arguments of types T and U and produces a result of type R.
- Method: R apply(T t, U u)
BiConsumer<T, U>
-
- Purpose: Represents an operation that accepts two input arguments and returns no result.
- Method: void accept(T t, U u)
BiPredicate<T, U>
-
- Purpose: Represents a predicate (boolean-valued function) of two arguments.
- Method: boolean test(T t, U u)
The java.util.function
package is a core part of Java’s functional programming capabilities, introduced in Java 8. It provides a rich set of functional interfaces that allow developers to write cleaner, more modular, and more expressive code—especially when working with lambdas, streams, and method references.