BinaryOperator is a functional interface that takes two Type T arguments and returns the same Type T object.
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
}
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
}
}
BinaryOperator inherits from BiFunction, and you can call apply()
to do something. It also provides static methods called minBy()
and maxBy()
.
public interface BiFunction<T, U, R> {
R apply(T t, U u);
}
Example 1 : BinaryOperator
BinaryOperator can be implemented as a Lambda expression. Passing two arguments to apply()
returns an object of the same type.
import java.util.function.BinaryOperator;
public class BinaryOperatorExample1 {
public static void main(String[] args) {
BinaryOperator<Integer> binaryOperator = (n1, n2) -> n1 + n2;
Integer sum = binaryOperator.apply(10, 100);
System.out.println(sum);
}
}
Output:
110
Example 2 : maxBy()
maxBy()
creates a BinaryOperator that compares two objects using a comparator and returns the larger value.
As shown in the code below, you need to implement the contents that Comparator compares and pass it as an argument.
Passing two objects to apply()
will return the larger object.
import java.util.function.BinaryOperator;
public class BinaryOperatorExample2 {
public static void main(String[] args) {
BinaryOperator<Item> binaryOperator = BinaryOperator.maxBy(
(Item i1, Item i2) -> i1.getId() - i2.getId());
Item item1 = new Item(10, "first");
Item item2 = new Item(20, "second");
Item max = binaryOperator.apply(item1, item2);
System.out.println(max);
}
}
// Item.java
public class Item {
private int id;
private String value;
public Item(int id, String value) {
this.id = id;
this.value = value;
}
public int getId() {
return id;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return "[id = " + id + ", value = " + value + "]";
}
}
Output:
[id = 20, value = second]
Example 3 : minBy()
minBy()
creates a BinaryOperator that compares two objects and returns the smaller value.
Implementation and usage are the same as maxBy()
.
import java.util.function.BinaryOperator;
public class BinaryOperatorExample3 {
public static void main(String[] args) {
BinaryOperator<Item> binaryOperator = BinaryOperator.minBy(
(Item i1, Item i2) -> i1.getId() - i2.getId());
Item item1 = new Item(10, "first");
Item item2 = new Item(20, "second");
Item min = binaryOperator.apply(item1, item2);
System.out.println(min);
}
}
Output:
[id = 10, value = first]
References
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)