Java 8 - BinaryOperator example

BinaryOperator is a functional interface that takes two Type T arguments and returns the same Type T object.

public interface BinaryOperator<T> extends BiFunction<T,T,T> {

  public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
      Objects.requireNonNull(comparator);
      return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
  }

  public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
      Objects.requireNonNull(comparator);
      return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
  }
}

BinaryOperator inherits from BiFunction, and you can call apply() to do something. It also provides static methods called minBy() and maxBy().

public interface BiFunction<T, U, R> {

      R apply(T t, U u);

}

Example 1 : BinaryOperator

BinaryOperator can be implemented as a Lambda expression. Passing two arguments to apply() returns an object of the same type.

import java.util.function.BinaryOperator;

public class BinaryOperatorExample1 {

    public static void main(String[] args) {

        BinaryOperator<Integer> binaryOperator = (n1, n2) -> n1 + n2;

        Integer sum = binaryOperator.apply(10, 100);

        System.out.println(sum);
    }
}

Output:

110

Example 2 : maxBy()

maxBy() creates a BinaryOperator that compares two objects using a comparator and returns the larger value. As shown in the code below, you need to implement the contents that Comparator compares and pass it as an argument.

Passing two objects to apply() will return the larger object.

import java.util.function.BinaryOperator;

public class BinaryOperatorExample2 {

    public static void main(String[] args) {

        BinaryOperator<Item> binaryOperator = BinaryOperator.maxBy(
                (Item i1, Item i2) -> i1.getId() - i2.getId());

        Item item1 = new Item(10, "first");
        Item item2 = new Item(20, "second");

        Item max = binaryOperator.apply(item1, item2);

        System.out.println(max);
    }
}
// Item.java

public class Item {
    private int id;
    private String value;
    public Item(int id, String value) {
        this.id = id;
        this.value = value;
    }

    public int getId() {
        return id;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "[id = " + id + ", value = " + value + "]";
    }
}

Output:

[id = 20, value = second]

Example 3 : minBy()

minBy() creates a BinaryOperator that compares two objects and returns the smaller value. Implementation and usage are the same as maxBy().

import java.util.function.BinaryOperator;

public class BinaryOperatorExample3 {

    public static void main(String[] args) {

        BinaryOperator<Item> binaryOperator = BinaryOperator.minBy(
                (Item i1, Item i2) -> i1.getId() - i2.getId());

        Item item1 = new Item(10, "first");
        Item item2 = new Item(20, "second");

        Item min = binaryOperator.apply(item1, item2);

        System.out.println(min);
    }
}

Output:

[id = 10, value = first]

References

codechachaCopyright ©2019 codechacha