In this post we’ll see examples of Java java.util.function.BinaryOperator
functional interface.
BinaryOperator functional interface represents an operation upon two operands of the same type, returning a result of the same type as the operands. BinaryOperator extends java.util.function.BiFunction interface and provides behavior for the case where the operands and the result are all of the same type. Since it extends BiFunction so inherits all the methods of the BiFunction interface-
- T apply(T t1, T t2)- Here T denotes the type of the arguments and the return type. This is the abstract method in this functional interface. If you are writing a Lambda expression that takes two arguments of same type and returns a value of the same type as passed arguments then that lambda expression can be written as an implementation of BinaryOperator built-in functional interface.
- andThen(Function<? super R,? extends V> after)- It takes another Function as argument and returns a composed BiFunction that performs, in sequence, first the operation of the calling BiFunction followed by the after operation.
Apart from the above two inherited methods BinaryOperator also has the following static methods.
- minBy(Comparator<? super T> comparator)- Returns a BinaryOperator which returns the lesser of two elements according to the specified Comparator.
- maxBy(Comparator<? super T> comparator)- Returns a BinaryOperator which returns the greater of two elements according to the specified Comparator.
BinaryOperator interface apply() method example
Here is a simple example where apply() method is implemented as a lambda expression that returns the sum of the passed integers.
import java.util.function.BinaryOperator; public class BinaryOperatorExample { public static void main(String[] args) { BinaryOperator<Integer> binaryOperator = (a,b) -> a+b; System.out.println("Result- " + binaryOperator.apply(2,3)); System.out.println("Result- " + binaryOperator.apply(9, 10)); } }Output
Result- 5 Result- 19
BinaryOperator interface andThen() method example
In the example passed arguments are added and then the result is squared and that is done as a sequence of operation using andThen() method.
import java.util.function.BinaryOperator; import java.util.function.Function; public class BinaryOperatorExample { public static void main(String[] args) { BinaryOperator<Integer> binaryOperator1 = (a,b) -> a+b; Function<Integer, Integer> function = (n) -> n*n; System.out.println("Result- " + binaryOperator1.andThen(function).apply(2,3)); System.out.println("Result- " + binaryOperator1.andThen(function).apply(9, 10)); } }Output
Result- 25 Result- 361
BinaryOperator interface minBy() and maxBy() methods example
Using the minBy() and maxBy() methods of the BinaryOperator we’ll get the employee with the minimum salary and with maximum salary from the list of employees. Both of these methods need a Comparator implementation. Note that both of these methods are implemented as static interface methods in BinaryOperator which means implementation is already there for these methods in the interface.
Employee classpublic class Employee { private String name; private String dept; private Integer salary; Employee(String name, String dept, Integer salary){ this.name = name; this.dept = dept; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSalary() { return salary; } public void setSalary(Integer salary) { this.salary = salary; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } }
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.function.BinaryOperator; import java.util.function.Function; public class BinaryOperatorExample { public static void main(String[] args) { List<Employee> employeeList = new ArrayList<>(); // Preparing List employeeList.add(new Employee("Jack", "Finance", 5500)); employeeList.add(new Employee("Lisa", "Finance", 5600)); employeeList.add(new Employee("Scott", "Finance", 7000)); employeeList.add(new Employee("Nikita", "IT", 4500)); employeeList.add(new Employee("Tony", "IT", 8000)); // Comparator implementation Comparator<Employee> comparator = (Employee e1, Employee e2) -> e1.getSalary().compareTo(e2.getSalary()); BinaryOperator<Employee> binOperatorMin = BinaryOperator.minBy(comparator); BinaryOperator<Employee> binOperatorMax = BinaryOperator.maxBy(comparator); Employee emp = findByComparison(employeeList, binOperatorMin); System.out.println("Employee with min salary: Name- " + emp.getName() + " Salary-" + emp.getSalary()); emp = findByComparison(employeeList, binOperatorMax); System.out.println("Employee with max salary: Name- " + emp.getName() + " Salary-" + emp.getSalary()); } public static Employee findByComparison(List<Employee> employeeList, BinaryOperator<Employee> binOperator){ Employee emp = null; for(Employee e: employeeList) { if(emp == null) { emp = e; } else { emp = binOperator.apply(emp, e); } } return emp; } }Output
Employee with min salary: Name- Nikita Salary-4500 Employee with max salary: Name- Tony Salary-8000
That's all for the topic BinaryOperator Functional Interface Java Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
- UnaryOperator Functional Interface Java Examples
- Supplier Functional Interface Java Examples
- Method Reference in Java
- Java Stream limit() Method With Examples
- Java Stream - Convert a Stream to List
- Instant in Java With Examples
- JShell in Java
- Read PDF in Java Using iText
- React Fragments With Examples
- Spring Boot + Spring Data REST Example