Java 8 Program To Find the Minimum and Maximum Number of a Stream

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.

Java 8 Program To Find the Minimum and Maximum Number of a Stream

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.

Comments