BinaryOperator maxBy example

Description

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

Syntax

maxBy has the following syntax.


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

Example

The following example shows how to use maxBy.


import java.util.Comparator;
import java.util.function.BinaryOperator;
//  w  ww.  j a va2s.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.