🎓 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
The min() method in Java, part of the java.util.stream.DoubleStream interface, is used to find the minimum element in a DoubleStream. This method is useful when you need to determine the lowest value in a stream of double values.
Table of Contents
- Introduction
min()Method Syntax- Understanding
min() - Examples
- Basic Usage
- Using
min()on an Empty Stream
- Real-World Use Case
- Conclusion
Introduction
The min() method is a terminal operation that returns an OptionalDouble describing the minimum element of the stream, or an empty OptionalDouble if the stream is empty.
min() Method Syntax
The syntax for the min() method is as follows:
OptionalDouble min()
Parameters:
- This method does not take any parameters.
Returns:
- An
OptionalDoubledescribing the minimum element of the stream, or an emptyOptionalDoubleif the stream is empty.
Throws:
- This method does not throw any exceptions.
Understanding min()
The min() method allows you to find the minimum value in a DoubleStream. The result is wrapped in an OptionalDouble to handle the case where the stream might be empty. If the stream contains elements, the OptionalDouble will contain the minimum value; otherwise, it will be empty.
Examples
Basic Usage
To demonstrate the basic usage of min(), we will create a DoubleStream and find the minimum element.
Example
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class MinExample {
public static void main(String[] args) {
DoubleStream doubleStream = DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5);
// Find the minimum element in the stream
OptionalDouble minElement = doubleStream.min();
// Print the minimum element if present
minElement.ifPresent(System.out::println);
}
}
Output:
1.1
Using min() on an Empty Stream
This example shows how to handle the case where the stream might be empty when using the min() method.
Example
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class EmptyStreamExample {
public static void main(String[] args) {
DoubleStream emptyStream = DoubleStream.empty();
// Find the minimum element in the empty stream
OptionalDouble minElement = emptyStream.min();
// Print the result
if (minElement.isPresent()) {
System.out.println("Minimum element: " + minElement.getAsDouble());
} else {
System.out.println("No element found in the stream.");
}
}
}
Output:
No element found in the stream.
Real-World Use Case
Finding the Minimum Temperature Reading
In real-world applications, the min() method can be used to find the lowest temperature reading from a stream of sensor data.
Example
import java.util.OptionalDouble;
import java.util.stream.DoubleStream;
public class SensorDataMinExample {
public static void main(String[] args) {
DoubleStream temperatureReadings = DoubleStream.of(25.3, 26.7, 24.8, 27.5, 30.1);
// Find the minimum temperature reading
OptionalDouble minTemperature = temperatureReadings.min();
// Print the minimum temperature if present
minTemperature.ifPresent(min -> System.out.println("Minimum temperature: " + min));
}
}
Output:
Minimum temperature: 24.8
Conclusion
The DoubleStream.min() method is used to find the minimum element in a stream of double values. This method is particularly useful for determining the lowest value in a stream. By understanding and using this method, you can efficiently manage and analyze streams of double values in your Java applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment