The DoubleSupplier interface in Java is part of the java.util.function package and is used to represent a supplier of double-valued results. It is a functional interface and is often used when we need to generate or supply double values (for example, random numbers or calculated values) without taking any input.
Functional Interface
Like other functional interfaces, DoubleSupplier is annotated with @FunctionalInterface and has a single abstract method:
double getAsDouble();
This method is used to obtain a double value whenever it is called.
How to Use DoubleSupplier
1. Using with Lambda Expressions
A common way to use DoubleSupplier is by defining the getAsDouble functionality using a lambda expression.
package org.kodejava.util.function;
import java.util.function.DoubleSupplier;
public class DoubleSupplierExample {
public static void main(String[] args) {
// Define a DoubleSupplier using a lambda expression
DoubleSupplier randomSupplier = () -> Math.random();
// Get values using the supplier
System.out.println("Random value 1: " + randomSupplier.getAsDouble());
System.out.println("Random value 2: " + randomSupplier.getAsDouble());
}
}
2. Using Method References
We can also implement DoubleSupplier using method references when there is a method compatible with the getAsDouble signature.
package org.kodejava.util.function;
import java.util.function.DoubleSupplier;
public class DoubleSupplierMethodReference {
public static void main(String[] args) {
// Use Math.random as a method reference for DoubleSupplier
DoubleSupplier randomSupplier = Math::random;
// Get and print values
System.out.println("Random value 1: " + randomSupplier.getAsDouble());
System.out.println("Random value 2: " + randomSupplier.getAsDouble());
}
}
3. Supplying Predefined Values
We can use DoubleSupplier to supply predefined double values. For instance, when iterating or producing specific values:
package org.kodejava.util.function;
import java.util.function.DoubleSupplier;
public class PredefinedDoubleSupplier {
public static void main(String[] args) {
// Supplier that always returns a specific value
DoubleSupplier constantSupplier = () -> 42.0;
// Get and print the value
System.out.println("Constant value: " + constantSupplier.getAsDouble());
}
}
4. Generating Calculated Values
DoubleSupplier can also be used in functional programming scenarios where a calculated double result is required:
package org.kodejava.util.function;
import java.util.function.DoubleSupplier;
public class CalculatedDoubleSupplier {
public static void main(String[] args) {
double initialValue = 10.0;
// e.g., applying a tax rate
DoubleSupplier calculatedSupplier = () -> initialValue * 1.21;
System.out.println("Calculated value: " + calculatedSupplier.getAsDouble());
}
}
5. Using in Streams
DoubleSupplier works seamlessly with primitive streams like DoubleStream:
package org.kodejava.util.function;
import java.util.function.DoubleSupplier;
import java.util.stream.DoubleStream;
public class DoubleStreamWithSupplier {
public static void main(String[] args) {
// Create a DoubleSupplier for an infinite stream of random numbers
DoubleSupplier randomSupplier = Math::random;
// Generate and print 5 random values
DoubleStream.generate(randomSupplier)
.limit(5)
.forEach(value -> System.out.println("Random value: " + value));
}
}
When to Use DoubleSupplier
- To generate values on demand (e.g., random numbers, calculated results).
- In functional programming contexts, often in combination with other functional constructs.
- As an alternative to avoid hardcoding or passing values directly when we want them generated lazily.
Summary
The DoubleSupplier interface is a convenient way to represent a source of double values. It is simple to use with lambda expressions, method references, or inline implementations. It shines in scenarios where values need to be generated dynamically, especially when combined with Streams or functional programming operations.
