Java Lambda - DoubleUnaryOperator compose example








DoubleUnaryOperator compose returns a composed operator that first applies the before operator to its input, and then applies this operator to the result.

Syntax

compose has the following syntax.

default DoubleUnaryOperator compose(DoubleUnaryOperator before)

Example

The following example shows how to use compose.

import java.util.function.DoubleUnaryOperator;
/*w ww.jav  a  2 s  .c om*/
public class Main {

  public static void main(String[] args) {
    DoubleUnaryOperator square = (x) -> {return x*x;};
    
    DoubleUnaryOperator doubleValue = (x) -> {return 2*x;};
    
    System.out.println(doubleValue.compose(square).applyAsDouble(3.14));
  }
}

The code above generates the following result.