DoubleUnaryOperator Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expects an argument of type double as input but produces the output of same type as input argument. In this post, we are going to see several implementations of DoubleUnaryOperator Interface by using different examples.

DoubleUnaryOperator_Interface_Java8_Techndeck

DoubleUnaryOperator Interface in Java 8 with Examples...!!! Share on X

Look at DoubleUnaryOperator Javadoc description below:

DoubleUnaryOperatorInterface_Signature_Java8_Techndeck

DoubleUnaryOperator Interface contains 4 methods:

  1. applyAsDouble

  2. compose

  3. andThen

  4. identity

Let’s discuss these methods:

identity

This method returns a double unary operator that always returns its input argument.

static DoubleUnaryOperator identity() {
        return t -> t;
    }

applyAsDouble 

This method performs operation on the given argument and return the function result.

double applyAsDouble(double operand);

Compose

This method returns a composed operator that first applies the before operator to its input, and then applies this operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the composed operator.

default DoubleUnaryOperator compose(DoubleUnaryOperator before) {
        Objects.requireNonNull(before);
        return (double v) -> applyAsDouble(before.applyAsDouble(v));
    }

andThen

This method returns a composed operator that first applies this operator to its input, and then applies the after operator to the result. If evaluation of either operator throws an exception, it is relayed to the caller of the composed operator.

default DoubleUnaryOperator andThen(DoubleUnaryOperator after) {
        Objects.requireNonNull(after);
        return (double t) -> after.applyAsDouble(applyAsDouble(t));
    }

Below are the several examples to demonstrate above methods:

Example 1. ‘applyAsDouble’

import java.util.function.DoubleUnaryOperator;

public class DoubleUnaryOperatorInterfaceJava8Example1 {

    public static void main(String[] args) {

        System.out.println("DoubleUnaryOperator Interface - 'applyAsDouble' example \n");

        DoubleUnaryOperator obj1 = d -> d * 5;

        System.out.println("Value of Obj 1: " + obj1.applyAsDouble(10.5));

    }
}

Output:

DoubleUnaryOperator Interface - 'applyAsDouble' example 

Value of Obj 1: 52.5

Example 2. ‘andThen’

import java.util.function.DoubleUnaryOperator;

public class DoubleUnaryOperatorInterfaceJava8Example2 {

    public static void main(String[] args) {

        System.out.println("DoubleUnaryOperator Interface - 'andThen' example \n");

        DoubleUnaryOperator obj1 = d - > d * 5;

        DoubleUnaryOperator obj2 = d - > d + 5;

        System.out.println("Result: " + obj1.andThen(obj2).applyAsDouble(10.5));


    }
}

Output:

DoubleUnaryOperator Interface - 'andThen' example 

Result: 57.5

Example 3. ‘compose’

import java.util.function.DoubleUnaryOperator;

public class DoubleUnaryOperatorInterfaceJava8Example3 {

    public static void main(String[] args) {

        System.out.println("DoubleUnaryOperator Interface - 'compose' example \n");

        DoubleUnaryOperator obj1 = d - > d * 5;

        DoubleUnaryOperator obj2 = d - > d + 5;

        System.out.println("Result: " + obj1.compose(obj2).applyAsDouble(10.5));

    }
}

Output:

DoubleUnaryOperator Interface - 'compose' example 

Result: 77.5

Java 8 DoubleUnaryOperator Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

DoubleUnaryOperator Interface in Java 8 with Examples...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts