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...!!! Click To Tweet

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.

applyAsDouble 

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

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.

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.

Below are the several examples to demonstrate above methods:

Example 1. ‘applyAsDouble’

Output:

Example 2. ‘andThen’

Output:

Example 3. ‘compose’

Output:

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...!!! Click To Tweet

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

Other Useful References: