🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, the DoubleSupplier interface is a functional interface that represents a supplier of double-valued results. It is part of the java.util.function package and is used to generate or supply a double value without any input.
Table of Contents
- What is
DoubleSupplier? - Methods and Syntax
- Examples of
DoubleSupplier - Real-World Use Case
- Conclusion
1. What is DoubleSupplier?
DoubleSupplier is a functional interface that provides a double result when called. It is commonly used in scenarios where a constant or calculated double value needs to be supplied on demand.
2. Methods and Syntax
The main method in the DoubleSupplier interface is:
double getAsDouble(): Gets adoubleresult.
Syntax
DoubleSupplier doubleSupplier = () -> {
// logic to return a double value
return result;
};
3. Examples of DoubleSupplier
Example 1: Returning a Constant Value
import java.util.function.DoubleSupplier;
public class ConstantDoubleSupplier {
public static void main(String[] args) {
// Define a DoubleSupplier that returns a constant value
DoubleSupplier constantValue = () -> 3.14;
double result = constantValue.getAsDouble();
System.out.println("Constant Value: " + result);
}
}
Output:
Constant Value: 3.14
Example 2: Generating a Random Double
import java.util.function.DoubleSupplier;
public class RandomDoubleSupplier {
public static void main(String[] args) {
// Define a DoubleSupplier that returns a random double
DoubleSupplier randomValue = () -> Math.random();
double result = randomValue.getAsDouble();
System.out.println("Random Value: " + result);
}
}
Output:
Random Value: [Random number between 0.0 and 1.0]
4. Real-World Use Case: Supplying Temperature Data
In monitoring systems, DoubleSupplier can be used to supply temperature readings from a sensor.
import java.util.function.DoubleSupplier;
public class TemperatureSensor {
public static void main(String[] args) {
// Define a DoubleSupplier to simulate temperature readings
DoubleSupplier temperatureReading = () -> getTemperature();
double temperature = temperatureReading.getAsDouble();
System.out.println("Current Temperature: " + temperature);
}
// Simulated method to get temperature
private static double getTemperature() {
// Logic to retrieve temperature data, e.g., from a sensor
return 25.0; // Assume a constant temperature for this example
}
}
Output:
Current Temperature: 25.0
Conclusion
The DoubleSupplier interface is used in Java for providing double values without any input parameters. It is particularly beneficial in scenarios like generating constant values, random numbers, or sensor data. Using DoubleSupplier can help create modular and reusable code, especially in functional programming contexts.
Comments
Post a Comment
Leave Comment