Java Binary Operator tutorials & examples
Understanding BinaryOperator in Java 8
The BinaryOperator
interface is part of the java.util.function
package in Java 8. This interface extends the BiFunction
interface and is designed for operations that take two parameters of the same type and return a result of the same type.
It is commonly used with lambda expressions or method references.
Syntax:
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T, T, T> {
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator);
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator);
}
Key Points:
- BinaryOperator extends
BiFunction<T, T, T>
indicating that it operates on two parameters of the same type T. - Two static methods,
minBy
andmaxBy
, are defined for finding the minimum and maximum based on a provided comparator.
BinaryOperator Lambda Expression Example
In this, Declare a Lambda function expression, that contains two parameters - (v1, v2) and, assigned to the BinaryOperator
function.
You can call apply a function to execute the BinaryOperator expression.
import java.util.function.BinaryOperator;
public class Main {
public static void main(String[] args) {
BinaryOperator<Integer> sum = (v1, v2) -> v1 + v2;
System.out.println(sum.apply(50, 70));
BinaryOperator<String> appendString = (str1, str2) -> str1 + str2;
System.out.println(appendString.apply("Binary ", "Operator Example "));
}
}
The output of the above code is
120
Binary Operator Example
BinaryOperator Method reference example
a static method is declared, this method is called using a double colon:: operator. Below is an example code for method reference usage.
package org.cloudhadoop.functions;
import java.util.function.BinaryOperator;
public class MyBinaryOperatorExample {
public static Integer sum(int m,int n) {
return m+n;
}
public static void main(String[] args) {
// lambda expression Example
BinaryOperator<Integer> sum=(v1, v2) ->sum(v1 , v2);
System.out.println(sum.apply(50, 70));
// Method reference Example
BinaryOperator<Integer> sum1= MyBinaryOperatorExample::sum;
System.out.println(sum1.apply(25,35));
}
}
Output:
120
60
BinaryOperator Static minBy() method Example
minBy()
method is a static method defined in the BinaryOperator
class of the java.util.function
package. It returns the Binary Operator that works in return low of given value based on the provided comparator.
import java.util.Comparator;
import java.util.function.BinaryOperator;
public class BinaryOperatorMinByExample {
public static void main(String[] args) {
BinaryOperator<Integer> naturalOrderMin = BinaryOperator.minBy(
Comparator.naturalOrder()); // Natural Order Comparator passed
BinaryOperator<Integer> reverseOrderMin = BinaryOperator.minBy(
Comparator.reverseOrder()); // Reverse Order Comparator passed
System.out.println(naturalOrderMin.apply(50, 45));
System.out.println(reverseOrderMin.apply(50, 45));
}
}
Output:
45
50
BinaryOperator maxBy() method Example
It is a static method that returns BinaryOperator
- which does a maximum of given values with the comparator provided.
import java.util.Comparator;
import java.util.function.BinaryOperator;
public class BinaryOperatorMaxByExample {
public static void main(String[] args) {
BinaryOperator<Integer> naturalOrderMax = BinaryOperator.maxBy(
Comparator.naturalOrder());
BinaryOperator<Integer> reverseOrderMax = BinaryOperator.maxBy(
Comparator.reverseOrder());
System.out.println(naturalOrderMax.apply(50, 45));
System.out.println(reverseOrderMax.apply(50, 45));
}
}
Output:
50
45
Conclusion
In summary, BinaryOperator
provides a concise way to perform operations on two parameters of the same type in Java 8. It is a versatile interface often used in conjunction with lambda expressions and method references.