This guide will probe into the powerful functionality of Java 8 BinaryOperator, a functional interface that represents an operation upon two operands of the same type, producing a result of the same type as the operands.
This article will explore maxBy()
and minBy()
methods, which allow us to find the maximum and minimum values respectively, based on a specified Comparator.
Additionally, it will also cover an example of IntBinaryOperator
, a specific implementation for handling integer values, and discuss Java’s fundamental binary operators for arithmetic and logical operations.
BinaryOperator Interface Basics
BinaryOperator
interface is a functional interface, meaning it has a single abstract method called apply()
.
When implementing this interface, you can pass lambda expression or method references to define the operation to be performed on the two input values.
With BinaryOperator
interface, you can define a function that operates on two operands of the same type and returns a result of the same type.
This interface is often used in scenarios where you need to combine two values into one, such as addition, multiplication, or comparison operations.
BinaryOperator
extends BiFunction<T, T, T>
, making it a functional interface with a single abstract method called apply()
. apply()
takes two operands of type T
and returns a result of type T
.
BinaryOperator
is a specialization of BiFunction
, which means that while BiFunction
can have operands and result of different type, BinaryOperator
should have all three of same type.
Adding integers with BinaryOperator
Below is an example to add two integers using BinaryOperator
BinaryOperator<Integer> func = (x1, x2) -> x1 + x2; Integer sum = func.apply(7, 5); System.out.println(sum); // 12
Likewise, we can do subtraction as well.
Concatenating strings with BinaryOperator
If we have two string values, then they can be joined or concatenated with BinaryOperator
as below
BinaryOperator<String> func = (s1, s2) -> s1.concat(s2); String result = func.apply("Java ", " 8!"); System.out.println(result); // Java 8!
Specialized BinaryOperator Interfaces
One of the specialized BinaryOperator
interfaces is IntBinaryOperator
, which is specifically designed to operate on two integer values.
This interface provides an applyAsInt()
method that takes two integers as input and returns an integer result.
Any code that requires performing arithmetic or bitwise operations on integer values can benefit from using IntBinaryOperator
.
Below example multiplies two integers and returns their product using IntOperator
.
IntBinaryOperator productOperation = (n1, n2) -> n1 * n2; int product = productOperation.applyAsInt(5, 2); System.out.println(product); // 10
Likewise, there are LongBinaryOperator
, DoubleBinaryOperator
interfaces that work for long
and double
values.
Practical Examples of BinaryOperator
maxBy() and minBy()
BinaryOperator contains methods that are useful to find the maximum or minimum value.
These methods are maxBy()
and minBy()
and are static.maxBy()
returns a BinaryOperator
that returns the greater of two elements based on a Comparator, while minBy()
returns the lesser of two elements based on a Comparator.
Both maxBy()
and minBy()
methods take a Comparator
as an argument, which defines the ordering to be used when comparing elements.
Find longer string
An example of using maxBy()
would be to find longer of two strings by comparing their lengths.
BinaryOperator<String> maxByLength = BinaryOperator. maxBy(Comparator.comparing(String::length)); String longer = maxByLength.apply("Tom", "Jerry"); System.out.println("Longer string: " + longer); // Jerry
Find shorter string
Similarly, minBy()
can be used to find the shorter string out of the two
BinaryOperator<String> minByLength = BinaryOperator. minBy(Comparator.comparing(String::length)); String shorter = minByLength.apply("Tom", "Jerry"); System.out.println("Shorter string: " + shorter); // Tom
Summing up
With these considerations, we have explored the concepts of binary operators in Java, focusing on maxBy()
and minBy()
examples.
Additionally, we have looked into IntBinaryOperator
as an example of a specific type of binary operator in Java.
Hope the article was useful.