January 7, 2019

Implementing java.util.function.DoubleBinaryOperator in java – functional programming.

DoubleBinaryOperator is a Functional Interface . It has only one abstract method and has no default method, unlike many other functional interfaces.

It is a primitive type specialization of BinaryOperator. If we are trying to implement BinaryOperator for a double type, rather than doing that, we can use DoubleBinaryOperator for that.

It has only one function. And this function is very simple. It takes two double argument and returns one double value.

  1. double applyAsDouble(double left, double right) :- It takes two double and returns one double. 🙂  hahaha

Now, we are going to implement this interface using Lambda Functions.

We are going to try simple functions, which does following operations – addition, multiplication and division.

I am trying to give a simple overview of the way this interface can be used.

And, one more thing, these functional interfaces with lambdas should be used to implement simple one liner solutions only. If your lambda grows into a block, then that code should be put in a function block.

Ok..here we go.

Addition :- 

//Get the sum of double                                                                       
DoubleBinaryOperator sum = (dbl1, dbl2) -> dbl1 + dbl2;

Multiplication :- 

//multiplication of double                                                                    
DoubleBinaryOperator mul = (dbl1, dbl2) -> dbl1 * dbl2;

Division :-

// division of double                                                                         
DoubleBinaryOperator div = (dbl1, dbl2) -> dbl1 / dbl2;

Instead of calling applyAsDouble on these lambdas each time, we can pass these lambdas in a utility function. And that will do the job for us.

Let us create that utility function. In this function, we will pass the lambda and the operands as the inputs.

private static double operateOn(DoubleBinaryOperator operator, double dbl1, double dbl2) {        
    return operator.applyAsDouble(dbl1, dbl2);                                                    
}

The above defined function is a static function, this makes testing easier, as I don’t have to create any object for that.

If you want, you can put it in a utility file as a static or member function.

Let us see the way, we are going to call this function.

operateOn(sum, 12, 13);
operateOn(mul, 12, 13)
operateOn(div, 12, 13)

You can see, we have passed lambdas and operands in our static function.

Next requirement is to use this function. How are we gonna use it. We can store the return value in some variable or put it into a map or add in a list – that depends on the requirement.

But, for testing purposes, I am going to put in a print statement like this.

System.out.println("Sum = " + operateOn(sum, 12, 13));                                        
System.out.println("Multiplication = " + operateOn(mul, 12, 13));                             
System.out.println("Division = " + operateOn(div, 12, 13));

let us run this code.

Output :-

Sum = 25.0
Multiplication = 156.0
Division = 0.9230769230769231

Ok, now you can have the complete code.

import java.util.function.DoubleBinaryOperator;                                                       
                                                                                                      
public class DoubleBinaryOperatorTest {                                                               
    public static void main(String ... args) {                                                        
        //Get the sum of double                                                                       
        DoubleBinaryOperator sum = (dbl1, dbl2) -> dbl1 + dbl2;                                       
        //multiplication of double                                                                    
        DoubleBinaryOperator mul = (dbl1, dbl2) -> dbl1 * dbl2;                                       
        // division of double                                                                         
        DoubleBinaryOperator div = (dbl1, dbl2) -> dbl1 / dbl2;                                       
                                                                                                      
        //You can call these lambdas directly but, that will not ba right way to do it                
        // We can create a function that takes a DoubleBinaryOperator and                             
        //applies it on the given input variables                                                     
        System.out.println("Sum = " + operateOn(sum, 12, 13));                                        
        System.out.println("Multiplication = " + operateOn(mul, 12, 13));                             
        System.out.println("Division = " + operateOn(div, 12, 13));                                   
                                                                                                      
    }                                                                                                 
                                                                                                      
    private static double operateOn(DoubleBinaryOperator operator, double dbl1, double dbl2) {        
        return operator.applyAsDouble(dbl1, dbl2);                                                    
    }                                                                                                 
}