BinaryOperator Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects two input arguments of same type as input but produces the output of same type as input arguments. In this post, we are going to see several implementations of BinaryOperator Interface by using different examples.

Also, BinaryOperator extends BiFunction interface therefore, it inherits apply() and andThen() functional methods from  the BiFunction.

BinaryOperator_Interface_Java8_Techndeck

BinaryOperator Interface in Java 8 with Examples...!!! Share on X

Look at BinaryOperator Javadoc description below:

BinaryOperatorInterface_Signature_Java8_Techndeck

 

 

 

 

 

BinaryOperator Interface contains 2 methods of it’s own and 2 extended methods:

  1. minBy

  2. maxBy

  3. apply (extended from BiFunction)

  4. andThen (extended from BiFunction)

 

Let’s discuss these methods:

minBy

This method returns a BinaryOperator which returns the lesser of two elements according to the specified Comparator.

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

maxBy

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

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

apply 

This method performs operation on the given arguments and return the function result.

R apply(T t, U u);

andThen

This method returns a composed function that first applies this function to its input, and then applies the after function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.

default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }

 

Below are the several examples to demonstrate above methods:

Example 1. ‘apply’

import java.util.function.BinaryOperator;

public class BinaryOperatorInterfaceJava8Example1 {

    public static void main(String[] args) {

        System.out.println("BinaryOperator Interface - 'apply' example \n");

        BinaryOperator <Integer> binaryOperator1 = (i1, i2) -> i1 * i2;
        System.out.println("Value of BinaryOperator 1: " + binaryOperator1.apply(5, 7));

        BinaryOperator <String> binaryOperator2 = (s1, s2) -> s1 + s2;
        System.out.println(binaryOperator2.apply("Value of BinaryOperator 2: " + "Techndeck - ", "For Programmers & Entrepreneurs"));

    }
}

Output:

BinaryOperator Interface - 'apply' example 

Value of BinaryOperator 1: 35
Value of BinaryOperator 2: Techndeck - For Programmers & Entrepreneurs

 

Example 2. ‘andThen’

import java.util.function.BinaryOperator;
import java.util.function.Function;

public class BinaryOperatorInterfaceJava8Example2 {

	public static void main(String[] args) {

		System.out.println("BinaryOperator Interface - 'andThen' example \n");
		
		BinaryOperator<Integer> binaryOperatorObj = (i1,i2) -> i1*i2;
		
		Function<Integer, Integer> functionObj = (i) -> i/2;
		
		System.out.println("Result of multiplication of (10,5) and then divided by 2 is: "+binaryOperatorObj.andThen(functionObj).apply(10,5));
		
	}

}

Output:

BinaryOperator Interface - 'andThen' example 

Result of multiplication of (10,5) and then divided by 2 is: 25

 

Example 3. ‘minBy’

import java.util.function.BinaryOperator;

public class BinaryOperatorInterfaceJava8Example3 {
    public static void main(String[] args) {

        System.out.println("BinaryOperator Interface - 'minBy' example \n");

        //Anonymous innerclass Implementation of Comparator
        /*
         * BinaryOperator<Integer> binaryOperatorObj = BinaryOperator.minBy(new
         * Comparator<Integer>() {
         * 
         * @Override public int compare(Integer o1, Integer o2) { return
         * o1.compareTo(o2); }
         * 
         * });
         */

        //Lambda Implementation of above code from line 13 to 19
        BinaryOperator <Integer> binaryOperatorObj = BinaryOperator.minBy((o1, o2) -> o1.compareTo(o2));

        System.out.println("Minimum value among 30 and 99: " + binaryOperatorObj.apply(30, 99));
    }
}

Output:

BinaryOperator Interface - 'minBy' example 

Minimum value among 30 and 99: 30

 

Example 4. ‘maxBy’

import java.util.function.BinaryOperator;

public class BinaryOperatorInterfaceJava8Example4 {
    public static void main(String[] args) {

        System.out.println("BinaryOperator Interface - 'maxBy' example \n");

        //Anonymous innerclass Implementation of Comparator
        /*
         * BinaryOperator<Integer> binaryOperatorObj = BinaryOperator.maxBy(new
         * Comparator<Integer>() {
         * 
         * @Override public int compare(Integer o1, Integer o2) { return
         * o1.compareTo(o2); } });
         */

        //Lambda Implementation of above code from line 13 to 19
        BinaryOperator <Integer> binaryOperatorObj = BinaryOperator.maxBy((o1, o2) -> o1.compareTo(o2));

        System.out.println("Maximum value among 30 and 99: " + binaryOperatorObj.apply(30, 99));
    }
}

Output:

BinaryOperator Interface - 'maxBy' example 

Maximum value among 30 and 99: 99

 

Java 8 BinaryOperator Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

 

BinaryOperator Interface in Java 8 with Examples...!!! Share on X

 

Do you like this Post? – then check my other helpful posts:

Other Useful References:

 

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts