Course Content
Stream API
Stream API
BinaryOperator: Combining Two Values
This interface is particularly useful when combining two values of the same type, such as performing mathematical calculations or merging data.
The BinaryOperator<T>
interface extends BiFunction<T, T, T>
, but unlike BiFunction
, both input arguments and the return value must always be of the same type T
.
Its only abstract method, apply(T t1, T t2)
, executes an operation on two arguments and returns the result.
Practical Example
Let's say you have a payroll system where you need to determine which of two bonuses — annual or quarterly — is higher so that the larger amount can be awarded to an employee.
Main
package com.example; import java.util.function.BinaryOperator; public class Main { public static void main(String[] args) { BinaryOperator<Double> maxBonus = (bonus1, bonus2) -> Math.max(bonus1, bonus2); double annualBonus = 5000.0; double quarterlyBonus = 1500.0; double finalBonus = maxBonus.apply(annualBonus, quarterlyBonus); System.out.println("Awarded Bonus: " + finalBonus); } }
In this example, you create a BinaryOperator<Double>
that utilizes Math.max()
to compare two values and return the larger one.
You then define two bonuses: annualBonus
(annual bonus) and quarterlyBonus
(quarterly bonus).
The apply()
method is called to determine the higher bonus, and the result is printed to the console. This approach is useful when you need to dynamically select the best option without complex conditional logic.
Static Methods
In addition to its primary apply()
method, BinaryOperator<T>
includes several useful static methods:
maxBy(Comparator<T> comparator)
– returns aBinaryOperator<T>
that selects the greater of two values based on the providedComparator<T>
;minBy(Comparator<T> comparator)
– returns aBinaryOperator<T>
that selects the smaller of two values based on the providedComparator<T>
.
Example Usage
Let's say you have an employee management system where you need to determine which of two candidates has a higher rating in order to select the best one for a promotion.
Main
package com.example; import java.util.Comparator; import java.util.function.BinaryOperator; public class Main { public static void main(String[] args) { BinaryOperator<Employee> topPerformer = BinaryOperator.maxBy(Comparator.comparingInt(Employee::getRating)); Employee employee1 = new Employee("John", 85); Employee employee2 = new Employee("Sarah", 90); Employee bestEmployee = topPerformer.apply(employee1, employee2); System.out.println("Top employee: " + bestEmployee.getName()); } } class Employee { private String name; private int rating; public Employee(String name, int rating) { this.name = name; this.rating = rating; } public String getName() { return name; } public int getRating() { return rating; } }
The BinaryOperator.maxBy()
method takes a Comparator<Employee>
, which uses Employee::getRating
to compare employees by their ratings.
Two Employee
objects are then created with different ratings. The apply()
method selects the employee with the highest rating and prints their name.
1. What is the main purpose of the BinaryOperator<T>
functional interface?
2. What is the primary method in the BinaryOperator<T>
interface?
3. Which interface is the parent of BinaryOperator<T>
?
4. What will the following code output?
Thanks for your feedback!