Java Stream How to - Create BinaryOperator from Lambda








Question

We would like to know how to create BinaryOperator from Lambda.

Answer

/* w ww .  j a v a2 s.  c  om*/

import java.util.function.BinaryOperator;

public class Main {
   public static void main(String[] args) {
      BinaryOperator<Integer> adder = (n1, n2) -> n1 + n2;

      System.out.println(adder.apply(3, 4));
   }
}

The code above generates the following result.