Java Stream min() Example

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 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 (176K+ 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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare