The DoubleFunction
interface in Java is a functional interface in the java.util.function
package. It represents a function that takes in a double
as an argument and produces a result of some type. Since it is a functional interface, you can use it as a lambda expression or method reference.
Method in DoubleFunction
The DoubleFunction
interface has one abstract method:
R apply(double value);
value
: Thedouble
value passed as input to the function.- The method returns an object of type
R
(the return type).
How to Use the DoubleFunction Interface
We typically use DoubleFunction
in situations where we need to transform or process a double
and produce a result of a specific type.
Example 1: Using DoubleFunction to Convert double
to a String
package org.kodejava.util.function;
import java.util.function.DoubleFunction;
public class DoubleFunctionExample {
public static void main(String[] args) {
// Create a DoubleFunction that converts a double to a String representation
DoubleFunction<String> doubleToString = (value) -> "Value is: " + value;
// Use the DoubleFunction
String result = doubleToString.apply(42.5);
// Print the result
// Output: Value is: 42.5
System.out.println(result);
}
}
Example 2: Using DoubleFunction to Compute Complex Results
You can use DoubleFunction
to compute and return various types of results, such as objects of a custom type.
package org.kodejava.util.function;
import java.util.function.DoubleFunction;
public class DoubleFunctionExample2 {
public static void main(String[] args) {
// Create a DoubleFunction that creates an Area object from a radius
DoubleFunction<Area> calculateArea =
(radius) -> new Area(Math.PI * radius * radius);
// Compute the area using the DoubleFunction
Area area = calculateArea.apply(5.0);
// Print the result
// Output: Area is: 78.53981633974483
System.out.println("Area is: " + area.getValue());
}
}
// A simple class to store area
class Area {
private double value;
public Area(double value) {
this.value = value;
}
public double getValue() {
return value;
}
}
Example 3: Method References with DoubleFunction
You can simplify usage by using method references when possible.
package org.kodejava.util.function;
import java.util.function.DoubleFunction;
public class DoubleFunctionExample3 {
public static void main(String[] args) {
// Using a method reference for Math.sqrt
DoubleFunction<Double> sqrtFunction = Math::sqrt;
// Apply the DoubleFunction
double result = sqrtFunction.apply(25.0);
// Print the result
// Output: Square root is: 5.0
System.out.println("Square root is: " + result);
}
}
Use Cases
- Converting a
double
to a custom object (e.g.,Area
,Volume
). - Performing mathematical computations with a
double
input and returning relevant results. - Simplifying transformations in streams or other functional programming contexts.
Key Points to Note
DoubleFunction<R>
is particularly useful to avoid unnecessary boxing of primitivedouble
values when dealing with functions that process them since it directly deals with primitive types.- It produces a result of any type
R
, which makes it very flexible.
By using DoubleFunction
, we can write concise, reusable, and readable code for operations that require processing a double
to produce a result.