BinaryOperator Functional Interface in Java

BinaryOperator interface in Java is another Functional interface introduced in version 8, and it is part of the java.util.function package. It extends BiFunction Functional interface. We covered the BiFunction interface in previous lessons. 

BinaryOperator accepts two inputs and returns the output.

BinaryOperator<T>

@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
    /**
     * Returns a {@link BinaryOperator} which returns the lesser of two elements
     * according to the specified {@code Comparator}.
     *
     * @param <T> the type of the input arguments of the comparator
     * @param comparator a {@code Comparator} for comparing the two values
     * @return a {@code BinaryOperator} which returns the lesser of its operands,
     *         according to the supplied {@code Comparator}
     * @throws NullPointerException if the argument is null
     */
    public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }

    /**
     * Returns a {@link BinaryOperator} which returns the greater of two elements
     * according to the specified {@code Comparator}.
     *
     * @param <T> the type of the input arguments of the comparator
     * @param comparator a {@code Comparator} for comparing the two values
     * @return a {@code BinaryOperator} which returns the greater of its operands,
     *         according to the supplied {@code Comparator}
     * @throws NullPointerException if the argument is null
     */
    public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }
}

In addition to the inherited methods from the BiFunction interface, it has two static methods.

Implementing the BinaryOperator interface in Java

Example 1:

Implementing the inherited abstract method R apply(T t, U u) from the BiFunction interface

class Test {

  public static void main(String[] args) {
    BinaryOperator<Integer> multiply = (num1, num2) -> num1 * num2;
    System.out.println("The result is: " + multiply.apply(5, 10));
  }
}

Output: The result is: 50

Example 2:

Implementing the static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) that accepts the Comparator and returns the BinaryOperator. 

class Test {

  public static void main(String[] args) {
    Comparator<Integer> comparator = (num1, num2) -> num1.compareTo(num2);
    BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(comparator);

    System.out.println("The result is: " + maxBy.apply(50, 51));
  }
}

Output: The result is: 51

Here, we created a Comparator and passed it as a parameter to the static maxBy() method, and we got the greater number of the two inputs in the output.

Example 3:

Implementing the static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) method that accepts the Comparator and returns the BinaryOperator just like the maxBy() method. The difference is that minBy() method will return the smaller number of two inputs.

class Test {

  public static void main(String[] args) {
    Comparator<Integer> comparator = (num1, num2) -> num1.compareTo(num2);
    BinaryOperator<Integer> minBy = BinaryOperator.minBy(comparator);

    System.out.println("The result is: " + minBy.apply(50, 51));
  }
}

Output: The result is: 50

I hope this tutorial was helpful to you. To learn more, check out other Java Functional Programming tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *