An easy way to understand BinaryOperator functional Interface in Java8.
Before studying this BinaryOperator , let’s look at UnaryOperator.
UnaryOperator:
Takes one argument and returns a value of the same type.
Represents operations that are performed on single values.
Extends the Function interface with the additional constraint that the input and output types are the same.
Common use cases include:
Incrementing a number
Converting a string to uppercase
Applying a mathematical function
BinaryOperator:
Takes two arguments of the same type and returns a value of the same type.
Represents operations that are performed on pairs of values.
Extends the BiFunction interface with the additional constraint that all three types (input1, input2, output) are the same.
Common use cases include:
Adding two numbers
Comparing two strings
Combining two objects
Here’s a table summarizing the key differences:
BinaryOperator interface extends BiFunction interface.
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
BiFunction interface apply(T t, U u) method.
BiFunction interface apply(T t, U u) method.
@FunctionalInterface
public interface BiFunction<T, U, R> {
/**
* Applies this function to the given arguments.
*
* @param t the first function argument
* @param u the second function argument
* @return the function result
*/
R apply(T t, U u);
Before using Lambdas and streams, let’s look at the traditional way .
import java.util.function.BinaryOperator;
public class BinaryOperatorClassic implements BinaryOperator<Integer> {
@Override
public Integer apply(Integer integer, Integer integer2) {
return integer + integer2;
}
public static void main(String[] args) {
BinaryOperatorClassic binaryOperatorClassic = new BinaryOperatorClassic();
// Test the apply method
Integer result = binaryOperatorClassic.apply(5, 3);
System.out.println("Result of addition: " + result); // Output: 8
}
}
Now lets try with lambda
public class BinaryOperatorLambda {
public static void main(String[] args) {
// Lambda expression for division
BinaryOperator<Integer> divide = (a, b) -> {
double divisionResult = (double) a / b;
// Round the result, and return it as an integer
return (int) Math.round(divisionResult);
};
/*
Here, (double) a / b
Perform division using double cast.
mentioning (double) before performing the division operation ensures that the division is performed using floating-point arithmetic,
which is necessary for getting a precise result when dividing integers
*/
// Test the divide method
int divideResult = divide.apply(10, 3);
// Print the result of the division
System.out.println("Result of division: " + divideResult); // Output: 3
}
}
This code calculates the sum of a list of integers using Java 8 streams and a BinaryOperator for addition in a concise manner.
this code demonstrates the usage of streams and functional interfaces in Java 8 to calculate the sum of a list of integers.
It showcases how to define a BinaryOperator for addition, use the reduce() method to perform reduction operations on streams, and obtain the desired result.
package steams.examples;
import java.util.Arrays;
import java.util.List;
import java.util.function.BinaryOperator;
public class BinaryOperatorStream {
public static void main(String[] args) {
// Sample list of integers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Define a BinaryOperator for addition
BinaryOperator<Integer> add = (a, b) -> a + b;
// Use reduce() method of Stream to calculate the sum
// The reduce() method takes an initial value (0) and a BinaryOperator (add)
// It applies the BinaryOperator to each element of the stream, starting with the initial value
// The result of each operation becomes the left operand of the next operation
int sum = numbers.stream().reduce(0, add);
// Print the sum
System.out.println("Sum of numbers: " + sum); // Output: Sum of numbers: 15
}
}