🎓 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
This tutorial focuses on the min() method of the Java Stream API. The min() method is used to determine the minimum element of a stream based on a given comparator. This method is particularly useful for finding the smallest item in a collection of data when working with streams.
Key Points
1. The min() method returns the smallest element in the stream as per the specified comparator.
2. It returns an Optional because the stream could be empty.
3. The Java Stream min() method is a terminal operation that returns the smallest element in the Stream.
2. Program Steps
1. Create a Stream of integers.
2. Apply the min() method with a comparator to find the smallest number.
3. Handle the result wrapped in an Optional.
3. Code Program
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;
public class StreamMinExample {
public static void main(String[] args) {
// Stream of integers
Stream<Integer> numberStream = Stream.of(10, 45, 75, 30, 90);
// Find min in stream of numbers
Optional<Integer> minNumber = numberStream.min(Comparator.naturalOrder());
minNumber.ifPresent(min -> System.out.println("The minimum number is: " + min));
// Stream of strings
Stream<String> stringStream = Stream.of("Apple", "Orange", "Banana", "Lemon", "Peach");
// Find min in stream of strings
Optional<String> shortestString = stringStream.min(Comparator.comparingInt(String::length));
shortestString.ifPresent(shortest -> System.out.println("The shortest string is: " + shortest));
}
}
Output:
The minimum number is: 10 The shortest string is: Peach
Explanation:
1. Stream.of(10, 45, 75, 30, 90) creates a stream of integers.
2. numberStream.min(Comparator.naturalOrder()) uses the natural ordering of integers to find the smallest value in the stream.
3. minNumber.ifPresent(min -> System.out.println("The minimum number is: " + min)) checks if a minimum value is present and prints it.
4. Stream.of("Apple", "Orange", "Banana", "Lemon", "Peach") creates a stream of strings.
5. stringStream.min(Comparator.comparingInt(String::length)) uses a comparator based on string length to find the shortest string in the stream.
6. shortestString.ifPresent(shortest -> System.out.println("The shortest string is: " + shortest)) prints the shortest string if it exists.
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