Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn BinaryOperator: Combining Two Values | Fundamentals and Functional Capabilities of Stream API
Stream API
course content

Course Content

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
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 bonusesannual or quarterly — is higher so that the larger amount can be awarded to an employee.

java

Main

copy
123456789101112131415
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 a BinaryOperator<T> that selects the greater of two values based on the provided Comparator<T>;
  • minBy(Comparator<T> comparator) – returns a BinaryOperator<T> that selects the smaller of two values based on the provided Comparator<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.

java

Main

copy
1234567891011121314151617181920212223242526272829303132333435
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?

question mark

What is the main purpose of the BinaryOperator<T> functional interface?

Select the correct answer

question mark

What is the primary method in the BinaryOperator<T> interface?

Select the correct answer

question mark

Which interface is the parent of BinaryOperator<T>?

Select the correct answer

question mark

What will the following code output?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 15
We're sorry to hear that something went wrong. What happened?
some-alt