Java Stream How to - Combine Function using andThen








Question

We would like to know how to combine Function using andThen.

Answer

import java.util.function.Function;
/*from  w  w  w . java  2  s. c o  m*/
public class Main {
  public static void main(String... args) {
    Function<String, Integer> toInteger = Integer::valueOf;
    Function<String, String> backToString = toInteger.andThen(String::valueOf);

    System.out.println( backToString.apply("123"));
  }
}

The code above generates the following result.