The DoubleUnaryOperator interface is a functional interface from the java.util.function package, introduced in Java 8. It represents an operation that takes a double value as input and returns a double value as output.
- It is the primitive specialization of UnaryOperator for double values, avoiding the use of the Double wrapper class.
- Lambda expressions can be used to implement its main method, applyAsDouble().
Example : Program to use DoubleUnaryOperator to multiply a double v alue by 2.5.
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String[] args) {
DoubleUnaryOperator operation = n -> n * 2.5;
System.out.println(operation.applyAsDouble(8.0));
}
}
Output
20.0
Explanation: The lambda expression n -> n * 2.5 is executed through applyAsDouble().
Syntax
@FunctionalInterface
public interface DoubleUnaryOperator {
double applyAsDouble(double operand);
}
The applyAsDouble() method is the functional method of the interface.
Methods of DoubleUnaryOperator
The DoubleUnaryOperator interface provides methods for applying operations to double values and creating reusable functional operations. These methods include identity(), applyAsDouble(), andThen(), and compose(), which support direct and chained operations.
1. identity()
The identity() method returns an operator that produces the same double value supplied as input.
Syntax
static DoubleUnaryOperator identity()
It returns a DoubleUnaryOperator that returns its input unchanged.
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String[] args) {
DoubleUnaryOperator operation =
DoubleUnaryOperator.identity();
System.out.println(operation.applyAsDouble(18.5));
}
}
Output
18.5
Explanation: identity() returns the input value without applying any transformation.
2. applyAsDouble()
The applyAsDouble() method applies the operation represented by the operator to a double value.
Syntax
double applyAsDouble(double operand)
- Parameters: operand is the double value supplied to the operation.
- Returns: It returns the resulting double value.
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String[] args) {
DoubleUnaryOperator operation = n -> n / 2;
System.out.println(operation.applyAsDouble(15.0));
}
}
Output
7.5
Explanation: The lambda expression divides the supplied value by 2.
3. andThen()
The andThen() method creates a composed operator that executes the current operation first and the specified operation afterward.
Syntax
default DoubleUnaryOperator andThen(DoubleUnaryOperator after)
- Parameters: after represents the operation executed after the current operation.
- Returns: It returns a composed DoubleUnaryOperator.
- Exception: It throws NullPointerException when after is null.
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String[] args) {
DoubleUnaryOperator operation = n -> n + 4;
DoubleUnaryOperator result =
operation.andThen(n -> n * 2);
System.out.println(result.applyAsDouble(6.0));
}
}
Output
20.0
Explanation: First 4 is added to 6.0, and the resulting 10.0 is multiplied by 2.
4. compose()
The compose() method creates a composed operator that executes the specified operation first and the current operation afterward.
Syntax
default DoubleUnaryOperator compose(DoubleUnaryOperator before)
- Parameters: before represents the operation executed before the current operation.
- Returns: It returns a composed DoubleUnaryOperator.
- Exception: It throws NullPointerException when before is null.
import java.util.function.DoubleUnaryOperator;
public class GFG {
public static void main(String[] args){
DoubleUnaryOperator operation = n -> n - 5;
DoubleUnaryOperator result = operation.compose(n -> n * 3);
System.out.println(result.applyAsDouble(10.0));
}
}
Output
25.0
Explanation: First 10.0 is multiplied by 3, and then 5 is subtracted from the result.