Function (java.util.function.Function)
`Function<T, R>` is a functional interface that represents a transformation from an input value of type `T` to a result of type `R`. It is commonly used with lambdas and method references when code needs to accept behavior as a value.
`Function<T, R>` is a functional interface in the `java.util.function` package. Its single abstract method is `R apply(T t)`, so a function receives one value and produces another value, which may have a different type.
Because it has one abstract method, a lambda can implement it directly. For example, `Function<String, Integer> length = s -> s.length()` describes an operation that converts a `String` into an `Integer`.
Functions are useful when behavior should be passed into another method. They are especially common with collection and Stream APIs, such as `map`, where each input element is transformed into a corresponding output element.
`Function` also provides default composition methods such as `andThen` and `compose`. `andThen` applies the current function first and then another function to its result; `compose` applies another function first and then the current function.
A function does not require the transformation to be mathematically pure: its implementation can have side effects. However, when using functions in stream pipelines, keeping transformations free of side effects generally makes the code easier to reason about.
Key points
- `Function<T, R>` converts one input value into one output value.
- Its abstract method is `R apply(T t)`.
- A lambda can implement `Function` because it is a functional interface.
- `andThen` chains the current function before another function; `compose` reverses that order.
- `Function` is commonly used with `Stream.map`.
Interview questions
Why is `Function` useful with Java Streams?
A stream often needs to transform each element into another value, and `Function` represents exactly that operation. The `map` operation accepts a function describing how each element should be converted. For example, a stream of names can be mapped to their lengths. This separates the transformation logic from the stream operation itself.
What is `java.util.function.Function`?
`Function<T, R>` is a functional interface representing an operation that accepts one argument of type `T` and returns a value of type `R`. Its main abstract method is `apply`. Because it has one abstract method, it can be implemented with a lambda or method reference. It is commonly used when passing transformation logic as an argument.
What does `Function<T, R>` mean?
`T` represents the type of the input, while `R` represents the type of the result. For example, `Function<String, Integer>` accepts a `String` and produces an `Integer`. The two types can be the same, but they do not have to be. This generic typing lets the compiler check that the function is used with compatible values.
How would you use a `Function` with a lambda?
You declare the input and result types and assign a lambda whose parameter and return value match them. For example, `Function<String, Integer> length = s -> s.length()`. Calling `length.apply("Java")` invokes the lambda and returns `4`. The compiler infers the lambda parameter type from the declared `Function` type.
What is the difference between `compose` and `andThen`?
Both methods allow functions to be chained, but they specify different execution order. With `andThen`, the current function runs first and its result is passed to the next function. With `compose`, the supplied function runs first and its result is passed to the current function. The types must line up between the two transformations.