Java - Function interface chained together


A function may be composed of as many functions as you want.

You can chain lambda expressions to create a function in one expression.

A cast is provided to help the compiler.


import java.util.function.Function;

public class Main {
  public static void main(String[] args) {
    // Square the input, add one to the result, and square the result
    Function<Long, Long> chainedFunction = ((Function<Long, Long>) (x -> x * x)).andThen(x -> x + 1)
        .andThen(x -> x * x);/*from w  ww  . j a v  a 2s  .  c  om*/



Related Topic