Java Stream How to - Create Function lambda and call compose method








Question

We would like to know how to create Function lambda and call compose method.

Answer

import java.util.function.Function;
//from   w ww .  j  av  a2  s. c o m
public class Main {

  public static void main(String[] args) {
    Function<Integer, Integer> incrementer = (n) -> n + 1;
    Function<Integer, Integer> before = (n) -> n * 2;
    System.out.println(incrementer.compose(before).apply(2));
  }

}

The code above generates the following result.