Friday, August 17, 2018

Java8 - BinaryOperator interface examples | Functional Interface tutorials

BinaryOperator is an interface defined in java.util.function package of java8. This interface extends BiFunction interface. This can be assigned with Lamda expressions/method references This performs an operation that accepts two parameters of the same type and returns the same type value as a result. In this post, We are going to learn BinaryOperator interface and it's method usage and examples. 
Syntax
@FunctionalInterface
public interface BinaryOperator<T> extends BiFunction<T,T,T> {
public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator)
public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) 
}
There is no direct abstract method in this. Two static methods - minBy, maxBy are defined in BinaryOperator. BiFunction has a single abstract method - R apply(T t, U u);

 Lambda Expression Example 

Lambda expression has only two parameters - (v1, v2) and Lambda expression is assigned to BinaryOperator function
BinaryOperator<Integer> sum = (v1, v2) -> v1 + v2;
		System.out.println(sum.apply(50, 70));
		BinaryOperator<String> appendString = (str1, str2) -> str1 + str2;
		System.out.println(appendString.apply("Binary ", "Operator Example "));
Output of the above code is
120
Binary Operator Example 

Method reference example 

a static method is declared and this method is called using a double colon:: operator. The below is an example code for method reference usage
package org.cloudhadoop.functions;
import java.util.function.BinaryOperator;
public class MyBinaryOperatorExample {
	public static Integer sum(int m,int n) {
	     return m+n;
	   }
	public static void main(String[] args) {
		  //  lambda expression Example
		BinaryOperator<Integer> sum=(v1, v2) ->sum(v1 , v2);
		System.out.println(sum.apply(50, 70));
		  //  Method reference Example
		BinaryOperator<Integer> sum1= MyBinaryOperatorExample::sum;
	      System.out.println(sum1.apply(25,35));      
	}
}
Output is
120
60

Static minBy() method Example 

minBy() method is a static method defined in java.util.function.BinaryOperator class.  It returns Binary Operator which works in return low of given value based on the provided comparator
BinaryOperator<Integer> naturalOrderMin = BinaryOperator.minBy(
Comparator.naturalOrder()); // Natural Order Comparator passed
BinaryOperator<Integer> reverseOrderMin = BinaryOperator.minBy(
Comparator.reverseOrder()); // Reverse Order Comparator passed
System.out.println(naturalOrderMin.apply(50, 45));
System.out.println(reverseOrderMin.apply(50, 45));
Output is
45
50

maxBy() method Example

It is a static method which returns BinaryOperator - which does a maximum of given values with comparator provided
BinaryOperator<Integer> naturalOrderMax = BinaryOperator.maxBy(
Comparator.naturalOrder());
BinaryOperator<Integer> reverseOrderMax = BinaryOperator.maxBy(
Comparator.reverseOrder());
System.out.println(naturalOrderMax.apply(50, 45));
System.out.println(reverseOrderMax.apply(50, 45));
Output of above code is
50
45

Related article


EmoticonEmoticon