The BinaryOperator<T> interface is a specialized functional interface introduced in Java 8 and available in the java.util.function package. It represents an operation that takes two operands of the same type and returns a result of the same type.
- Specialized form of BiFunction<T, T, T>.
- Accepts two arguments of the same type.
- Commonly used for comparison, aggregation, and reduction operations.
import java.util.function.BinaryOperator;
public class Main {
public static void main(String[] args) {
BinaryOperator<Integer> add =
(a, b) -> a + b;
System.out.println(
"Sum = " + add.apply(10, 20)
);
}
}
Output
Sum = 30
Explanation: In this example, the BinaryOperator accepts two integers and returns their sum. Since the input and output types are the same (Integer), BinaryOperator is a suitable choice.
Syntax
@FunctionalInterface
public interface BinaryOperator<T>
extends BiFunction<T, T, T>
Here,
T:Type of both input arguments and the return value.
Methods of BinaryOperator Interface
1. maxBy() Method
The maxBy() method returns a BinaryOperator that selects the greater of two elements according to the specified Comparator. Returns the maximum of two values.
- Uses a Comparator for comparison.
- Useful for finding the largest element.
Syntax:
static <T> BinaryOperator<T>
maxBy(Comparator<? super T> comparator)
- Parameters
:comparator:Comparator used to compare two values. - Return Value: Returns a BinaryOperator that returns the larger of the two operands.
import java.util.function.BinaryOperator;
public class GFG {
public static void main(String args[])
{
BinaryOperator<Integer>
op = BinaryOperator
.maxBy(
(a, b) -> (a > b) ? 1 : ((a == b) ? 0 : -1));
System.out.println(op.apply(98, 11));
}
}
Output
98
Explanation: The maxBy() method compares 98 and 11 using Integer.compare(). Since 98 is greater, it is returned as the result.
2. minBy()
This method returns a BinaryOperator which returns the lesser of the two elements based on a given comparator
Syntax:
static <T> BinaryOperator<T>
minBy(Comparator<? super T> comparator)
Parameters: It takes in only one parameter namely, comparator which is an object of the Comparator class.
Returns: This method returns a BinaryOperator which return the minimum of the two objects passed while calling it based on the given comparator.
import java.util.function.BinaryOperator;
public class GFG {
public static void main(String args[])
{
BinaryOperator<Integer>
op = BinaryOperator
.minBy(
(a, b) -> (a > b) ? 1 : ((a == b) ? 0 : -1));
System.out.println(op.apply(98, 11));
}
}
Output
11
Explanation: The minBy() method compares 98 and 11 using Integer.compare(). Since 11 is smaller, it is returned as the result.
BinaryOperator vs BiFunction
| Feature | BinaryOperator | BiFunction |
|---|---|---|
| Input Arguments | Same Type | Can Be Different Types |
| Return Type | Same as Input Type | Can Be Different |
| Extends | BiFunction<T,T,T> | Functional Interface |
| Use Case | Comparison, Reduction | General Two-Input Operations |
| Package | java.util.function | java.util.function |
Advantages of BinaryOperator
- Simplifies operations where all types are the same.
- Reduces boilerplate code compared to BiFunction.
- Supports lambda expressions and method references.
- Useful for finding minimum and maximum values.
- Commonly used in Stream API reduction operations.
- Improves code readability and maintainability.