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 in Java 8 with Examples...!!! Click To Tweet
Look at BinaryOperator Javadoc description below:
BinaryOperator Interface contains 2 methods of it’s own and 2 extended methods:
minBy
maxBy
apply (extended from BiFunction)
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.
1 2 3 4 | 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.
1 2 3 4 | 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.
1 | 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.
1 2 3 4 | 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’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:
1 2 3 4 | BinaryOperator Interface - 'apply' example Value of BinaryOperator 1: 35 Value of BinaryOperator 2: Techndeck - For Programmers & Entrepreneurs |
Example 2. ‘andThen’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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:
1 2 3 | BinaryOperator Interface - 'andThen' example Result of multiplication of (10,5) and then divided by 2 is: 25 |
Example 3. ‘minBy’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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:
1 2 3 | BinaryOperator Interface - 'minBy' example Minimum value among 30 and 99: 30 |
Example 4. ‘maxBy’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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:
1 2 3 | 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...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
Other Useful References: