{

Java8 - BinaryOperator interface examples | Functional Interface tutorials


Java8 functional interface example

What is BinaryOperator in java8?

BinaryOperator is an interface defined in the java.util.function package in java8. This interface extends the BiFunction interface. It is used to perform an operation that accepts two parameters of the same type and returns the same type value as a result. It assigns Lamda expressions or method references.

Learn the BinaryOperator interface and its 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, and maxBy are defined in BinaryOperator. BiFunction has a single abstract method - R apply(T t, U u);

BinaryOperator Lambda Expression Example

In this, Declare a Lambda function expression, that contains two parameters - (v1, v2) and, assigned to the BinaryOperator function.

You can call apply a function to execute the BinaryOperator expression.

import java.util.function.BinaryOperator;

public class Main {
    public static void main(String[] args) {
        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 "));
    }
}

The output of the above code is

120  
Binary Operator Example 

BinaryOperator Method reference example

a static method is declared, this method is called using a double colon:: operator. 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:

120  
60  

BinaryOperator Static minBy() method Example

minBy() method is a static method defined in the BinaryOperator class of the java.util.function package. It returns the Binary Operator that works in return low of given value based on the provided comparator.

import java.util.Comparator;
import java.util.function.BinaryOperator;

public class BinaryOperatorMinByExample {
    public static void main(String[] args) {
        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:

45  
50  

BinaryOperator maxBy() method Example

It is a static method that returns BinaryOperator - which does a maximum of given values with the comparator provided.

import java.util.Comparator;
import java.util.function.BinaryOperator;

public class BinaryOperatorMaxByExample {
    public static void main(String[] args) {
        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:

50  
45  
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.