Java Reference ยท Streams and Functional
Function and BiFunction interfaces
Reference for java.util.function.Function and BiFunction: apply(), compose(), andThen(), identity(), generics and common uses with streams and maps.
Function<T, R> represents an operation that takes one argument of type T and produces a result of type R. Its single abstract method is R apply(T t). BiFunction<T, U, R> is the two-argument version, R apply(T t, U u).
Functions are the engine of Stream.map(), Optional.map(), Map.computeIfAbsent() and Comparator.comparing(). Because they are ordinary objects, you can store them in variables, pass them to methods and chain them with andThen() and compose().
Function<String, Integer> length = String::length; int n = length.apply("coffee"); // 6
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
@FunctionalInterface public interface Function<T, R> { R apply(T t); default <V> Function<V, R> compose(Function<? super V, ? extends T> before); default <V> Function<T, V> andThen(Function<? super R, ? extends V> after); static <T> Function<T, T> identity(); } @FunctionalInterface public interface BiFunction<T, U, R> { R apply(T t, U u); default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after); }
| Member | Description |
|---|---|
apply(t) |
Computes the result for the argument. |
andThen(after) |
Returns a function that applies this one first, then after to the result. |
compose(before) |
Returns a function that applies before first, then this one. |
identity() |
A function that always returns its argument. |
Returns: apply() returns the computed R; whether it may be null depends on the function, but many APIs (like Collectors.groupingBy) reject a null result.
Usage notes
andThenvs.compose:f.andThen(g)means "g after f";f.compose(g)means "f after g". Mixing them up is the usual bug.BiFunctionhas nocompose, because one function result cannot feed two arguments.- Related shapes: use
UnaryOperator<T>when input and output types are the same, andToIntFunctionand friends to avoid boxing. - Checked exceptions cannot be thrown from
apply(). Wrap the call in a try/catch inside the lambda or define your own interface. - Purity: functions passed to stream operations should be stateless and free of side effects.
- Method references of all four kinds fit:
String::length(unbound instance),Integer::parseInt(static),this::helperandPerson::new(constructor). - Currying:
Function<A, Function<B, C>>gives a curried two-argument function, thoughBiFunctionis clearer.