The DoubleUnaryOperator
functional interface in Java is part of the java.util.function
package and represents a functional interface for operations that accept a single double
value as input and produce a double
value as output. It is typically used in scenarios where mathematical or computational transformations need to be applied to a double
value.
Functional Interface
The DoubleUnaryOperator
interface has a single abstract method:
double applyAsDouble(double operand);
This method takes a double
value as an argument and returns a double
after performing the specified operation.
How to Use DoubleUnaryOperator
1. Using a Lambda Expression
We can use a lambda expression to implement the applyAsDouble
method for custom operations.
Example:
package org.kodejava.util.function;
import java.util.function.DoubleUnaryOperator;
public class DoubleUnaryOperatorExample {
public static void main(String[] args) {
// Define a DoubleUnaryOperator to square a value
DoubleUnaryOperator square = value -> value * value;
// Apply the operator
System.out.println("Square of 5.5: " + square.applyAsDouble(5.5));
// Output: Square of 5.5: 30.25
}
}
In this example, a lambda expression is used to compute the square of a value.
2. Using with Built-in Methods
We can leverage existing methods such as Math
operations with DoubleUnaryOperator
.
Example:
package org.kodejava.util.function;
import java.util.function.DoubleUnaryOperator;
public class BuiltInMethodsExample {
public static void main(String[] args) {
// Create a DoubleUnaryOperator using Math.sqrt
DoubleUnaryOperator squareRoot = Math::sqrt;
// Apply the operator
System.out.println("Square root of 36: " + squareRoot.applyAsDouble(36));
// Output: Square root of 36: 6.0
}
}
Here, the Math.sqrt
method is used as the implementation for the applyAsDouble
method.
3. Combining Operators
The DoubleUnaryOperator
interface provides useful default methods, such as andThen
and compose
, which enable chaining multiple operations.
andThen
: Executes the current operation and then another.compose
: Executes another operation first, then the current one.
Example:
package org.kodejava.util.function;
import java.util.function.DoubleUnaryOperator;
public class ChainingOperatorsExample {
public static void main(String[] args) {
// Define two DoubleUnaryOperators
DoubleUnaryOperator doubleValue = value -> value * 2;
DoubleUnaryOperator addTen = value -> value + 10;
// Chain the operators
DoubleUnaryOperator combined = doubleValue.andThen(addTen);
// Apply the combined operator
System.out.println("Result: " + combined.applyAsDouble(5));
// Output: Result: 20.0
}
}
In this example, the operand is first doubled (*2
) and then 10 is added.
4. Using with Streams
The DoubleUnaryOperator
is often used with streams, especially DoubleStream
, to perform transformations on a sequence of double
values.
Example:
package org.kodejava.util.function;
import java.util.stream.DoubleStream;
import java.util.function.DoubleUnaryOperator;
public class StreamExample {
public static void main(String[] args) {
// Create a stream of doubles
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3);
// Create a DoubleUnaryOperator for scaling
DoubleUnaryOperator scaleBy100 = value -> value * 100;
// Apply the operator to the stream
doubleStream.map(scaleBy100)
.forEach(result -> System.out.printf("%.1f%n", result));
// Output:
// 110.0
// 220.0
// 330.0
}
}
This example scales each value in the DoubleStream
by multiplying it by 100
.
Methods in DoubleUnaryOperator
applyAsDouble(double operand)
: The abstract method that is implemented to perform an operation on adouble
value.-
andThen(DoubleUnaryOperator after)
: Returns a composed operator that performs the current operation first, then applies theafter
operation. -
compose(DoubleUnaryOperator before)
: Returns a composed operator that performs thebefore
operation first, then applies the current operation. -
identity()
(Static Method): Returns an identity operator that always returns its input.
Example Using identity()
Method:
The identity()
method is useful when no changes are needed to the input, but we still want to use a functional-style approach.
package org.kodejava.util.function;
import java.util.function.DoubleUnaryOperator;
public class IdentityExample {
public static void main(String[] args) {
// Using the identity operator
DoubleUnaryOperator identity = DoubleUnaryOperator.identity();
// Apply the operator
double input = 42.0;
System.out.println("Identity result: " + identity.applyAsDouble(input));
// Output: Identity result: 42.0
}
}
Summary
The DoubleUnaryOperator
is highly versatile for performing transformations on double
values, including:
– Basic mathematical operations
– Combining multiple operations using andThen
and compose
– Functionally processing streams of double
values
It is often used in mathematical computations, data processing pipelines, and transformation functions in functional programming contexts.