BinaryOperator Interface in Java
The BinaryOperator Interface<T> is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a binary operator which takes two operands and operates on them to produce a result. However, what distinguishes it from a normal BiFunciton is that both of its arguments and its return type are same.
This functional interface which takes in one generic namely :-
- T: denotes the type of the input arguments and the return value of the operation
The BinaryOperator<T> extends the BiFunction<T, T, T> type. So it inherits the following methods from the BiFunction Interface:
The lambda expression assigned to an object of BiaryOperator type is used to define its apply() which eventually applies the given operation on its arguments.
Functions in BinaryOperator Interface
The BinaryOperator interface consists of the following functions:
1. maxBy()
This methos return a BinaryOperator which returns the greater of the two elements based on a given comparator
Syntax:
static <T> BinaryOperator<T>
maxBy(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 maximum of the two objects passed while calling it based on the given comparator.
Below is the code to illustrate maxBy() method:
Program:
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)); } } |
98
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.
Below is the code to illustrate minBy() method:
Program:
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)); } } |
11
Recommended Posts:
- Map Interface in Java
- Java 8 | Consumer Interface in Java with Examples
- Java 8 | IntToLongFunction Interface in Java with Examples
- Java 8 | DoubleToLongFunction Interface in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples
- Java 8 | IntToDoubleFunction Interface in Java with Examples
- IntUnaryOperator Interface in Java
- Queue Interface In Java
- Nested Interface in Java
- Java Interface methods
- LongUnaryOperator Interface in Java
- Map.Entry interface in Java with example
- DoubleUnaryOperator Interface in Java
- BlockingQueue Interface in Java
- Marker interface in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



