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 in Java 8 with Examples...!!! Click To Tweet
Look at DoubleUnaryOperator Javadoc description below:
DoubleUnaryOperator Interface contains 4 methods:
applyAsDouble
compose
andThen
identity
Let’s discuss these methods:
identity
This method returns a double unary operator that always returns its input argument.
1 2 3 | static DoubleUnaryOperator identity() { return t -> t; } |
applyAsDouble
This method performs operation on the given argument and return the function result.
1 | 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.
1 2 3 4 | 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.
1 2 3 4 | 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’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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:
1 2 3 | DoubleUnaryOperator Interface - 'applyAsDouble' example Value of Obj 1: 52.5 |
Example 2. ‘andThen’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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:
1 2 3 | DoubleUnaryOperator Interface - 'andThen' example Result: 57.5 |
Example 3. ‘compose’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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:
1 2 3 | DoubleUnaryOperator Interface - 'compose' example Result: 77.5 |
DoubleUnaryOperator Interface in Java 8 with Examples...!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8