🎓 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
1. Introduction
Finding the minimum and maximum values in a collection of numbers is a fundamental operation in data processing, useful in statistical analysis, data validation, and numerous other contexts where understanding the range of data is important. Java 8 introduced the Stream API, which simplifies the process of manipulating collections of data, including finding minimum and maximum values with more readability and less code compared to traditional approaches. This blog post will demonstrate how to use Java 8 streams to find the minimum and maximum numbers in a stream of integers.
2. Program Steps
1. Create a stream of integers.
2. Use the min() method of the Stream API to find the minimum value in the stream.
3. Use the max() method of the Stream API to find the maximum value in the stream.
4. Display the minimum and maximum values found.
3. Code Program
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class MinMaxFinder {
public static void main(String[] args) {
// Step 1: Creating a stream of integers
IntStream numbers = Arrays.stream(new int[]{10, 20, 3, 8, 50, 33});
// Step 2: Finding the minimum value
OptionalInt min = numbers.min();
// Since we can't reuse streams, creating another stream for the max operation
IntStream numbersForMax = Arrays.stream(new int[]{10, 20, 3, 8, 50, 33});
// Step 3: Finding the maximum value
OptionalInt max = numbersForMax.max();
// Step 4: Displaying the results
min.ifPresent(value -> System.out.println("Minimum value: " + value));
max.ifPresent(value -> System.out.println("Maximum value: " + value));
}
}
Output:
Minimum value: 3 Maximum value: 50
Explanation:
1. The program starts by creating an IntStream named numbers, which contains a series of integers. This stream acts as the source of data for finding the minimum and maximum values.
2. The min() method is called on the stream to find the minimum value. This method returns an OptionalInt, which is a container object that may or may not contain an int value. This is used because the original stream might be empty, in which case there would be no minimum value.
3. A new stream numbersForMax is created for finding the maximum value because streams in Java 8 are consumable; once terminated, they cannot be reused. The max() method works similarly to min(), finding the maximum value in the stream and returning it wrapped in an OptionalInt.
4. Finally, the program checks if the OptionalInt instances contain values using ifPresent() and prints the minimum and maximum values. This step ensures that values are only printed if the stream contained any numbers.
5. The output clearly displays the minimum and maximum values found in the stream, demonstrating the effectiveness of using Java 8's Stream API to perform aggregate operations on collections of data.
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