Java SE API

Function.compose()

Applies before to the input, then passes its result to this function. Rejects null before.

Formal Syntax & Structure

java syntaxjava
default <V> Function<V,R> compose(Function<? super V,? extends T> before)

Parameters & Attributes

NameTypeDescription
beforeFunction<? super V,? extends T>Transformation to run first; non-null

Return Value

Function<V,R>

Combined transformation

Working Example

Example Snippetjava
import java.util.*;import java.util.function.*;public class Main {
    public static void main(String[] args) {        Function<Integer, Integer> add = n -> n + 3;        Function<Integer, Integer> doubleIt = n -> n * 2;        System.out.println(add.andThen(doubleIt).apply(4));        System.out.println(add.compose(doubleIt).apply(4));    }}
Java Reference