Java Lambda - BinaryOperator maxBy example

BinaryOperator maxBy method returns a BinaryOperator which returns the greater of two elements according to the specified Comparator.


maxBy has the following syntax.

static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator)


The following example shows how to use maxBy.

import java.util.Comparator;
import java.util.function.BinaryOperator;
//from  ww  w  .  j  a v a  2 s  .  c  o m

public class Main {
   public static void main(String[] args) {
     BinaryOperator<Integer> bi = BinaryOperator.maxBy(Comparator.reverseOrder());
     System.out.println(bi.apply(2, 3));

The code above generates the following result.