Java 8 BinaryOperator Interface

10 months ago codedestine 0

In this tutorial, we will learn about Java 8 BinaryOperator interface.

Java 8 BinaryOperator Interface

BinaryOperator Interface :-

BinaryOperator ( java.util.function.BinaryOperator ) is a subinterface of the BiFunction interface, it has two static method along with all the abstract and default method inherited from the BiFunction interface. It represents an algorithm where two input parameter of same type is used to produce return value of the type same as passed input. This interface is a specialized form of BiFunction interface for the case where both passed argument and return value are of same type. 

Static method :- 

  1. static  <T>  BinaryOperator<T>  maxBy ( Comparator<? super T>  comparator ) :- It returns a BinaryOperator which returns the greater element between the two elements according to the specified Comparator.
  2. static  <T>  BinaryOperator<T>  minBy ( Comparator<? super T>  comparator ) :- It returns a BinaryOperator which returns the smaller element between the two elements according to the specified Comparator.

Reference :-  BinaryOperator Interface JavaDocs 

Java 8 Stream API have three method where BinaryOperator interface is used as an argument :- 

  1. Optional<T> reduce ( BinaryOperator<T> accumulator )
  2. <U> U reduce ( U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner )
  3. T reduce( T identity, BinaryOperator<T> accumulator )

There are three specialized ( primitive specific ) form of Java 8 BinaryOperator interface, which can be used in case of int, long and double. They are as follows :-

  1. IntBinaryOperator interface ( JavaDocs ) :- It represents a BinaryOperator that accepts two integer value as an argument and returns an integer value.
  2. DoubleBinaryOperator interface ( JavaDocs ) :- It represents a BinaryOperator that accepts two double value as an argument and returns a double value.
  3. LongBinaryOperator interface ( JavaDocs ) :- It represents a BinaryOperator that accepts two long value as an argument and returns a long value.

Example – 1 :- 

This example will show you, how to create and call different methods of an BinaryOperator interface.

  public static void main(String[] args){
 
    BinaryOperator<Integer> bOpertor = ( t, u ) -> u * t;
 
    // apply method Example
    int result = bOpertor.apply(10, 20);
    System.out.println(result); 
 
    // min method Example
    BinaryOperator<Integer> bOpertorMin = BinaryOperator.minBy((Integer t, Integer u) -> t.compareTo(u));
    result = bOpertorMin.apply(10,20);
    System.out.println(result);
 
    // max method Example
    BinaryOperator<Integer> bOpertorMax = BinaryOperator.maxBy((Integer t, Integer u) -> t.compareTo(u));
    result = bOpertorMax.apply(10,20);
    System.out.println(result);
 
  }

Result :- 

200
10
20

Example – 2 :- 

In this example, we will reduce the stream by using stream’s reduce() method. This method takes one BinaryOperator as an argument.

  public static void main(String[] args){
    List<Integer> intList = Arrays.asList(1, 2, 3, 4);
 
    // Map method Example
    int result = intList.stream().reduce((t,u) -> t + u ).get(); 
    System.out.println(result);

 }

Result :- 

10

That’s all for Java 8 BinaryOperator Interface. If you liked it, please share your thoughts in comments section and share it with others too.