Java IntStream min() Method

The min() method in Java, part of the java.util.stream.IntStream interface, is used to find the minimum element in the stream. This method is useful when you need to determine the smallest value in a stream of integers.

Table of Contents

  1. Introduction
  2. min() Method Syntax
  3. Understanding min()
  4. Examples
    • Basic Usage
    • Using min() with Filtered Streams
  5. Real-World Use Case
  6. Conclusion

Introduction

The min() method is a terminal operation that returns an OptionalInt describing the minimum element of the stream, or an empty OptionalInt if the stream is empty. This method is particularly useful when you need to find the smallest value in a sequence of integers.

min() Method Syntax

The syntax for the min() method is as follows:

OptionalInt min()

Parameters:

  • This method does not take any parameters.

Returns:

  • An OptionalInt describing the minimum element of the stream, or an empty OptionalInt if the stream is empty.

Throws:

  • This method does not throw any exceptions.

Understanding min()

The min() method processes the elements of the stream and returns the smallest value as an OptionalInt. If the stream contains no elements, it returns an empty OptionalInt.

Examples

Basic Usage

To demonstrate the basic usage of min(), we will create an IntStream and use min() to find the smallest element.

Example

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class MinExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(5, 3, 9, 1, 4);

        // Use min() to find the smallest element in the stream
        OptionalInt minElement = intStream.min();

        // Print the minimum element if present
        minElement.ifPresent(System.out::println);
    }
}

Output:

1

Using min() with Filtered Streams

This example shows how to use min() in combination with other stream operations, such as filtering.

Example

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class MinWithFilterExample {
    public static void main(String[] args) {
        IntStream intStream = IntStream.of(5, 3, 9, 1, 4, 8, 6, 7, 2);

        // Filter even numbers and find the minimum element
        OptionalInt minElement = intStream.filter(n -> n % 2 == 0).min();

        // Print the minimum element if present
        minElement.ifPresent(System.out::println);
    }
}

Output:

2

Real-World Use Case

Finding the Minimum Transaction Amount

In real-world applications, the min() method can be used to find the minimum transaction amount from a stream of transaction values.

Example

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class MinTransactionExample {
    public static void main(String[] args) {
        IntStream transactions = IntStream.of(150, 200, 450, 320, 500);

        // Use min() to find the smallest transaction amount
        OptionalInt minTransaction = transactions.min();

        // Print the minimum transaction amount if present
        minTransaction.ifPresent(amount -> System.out.println("Minimum transaction amount: " + amount));
    }
}

Output:

Minimum transaction amount: 150

Conclusion

The IntStream.min() method is used to find the minimum element in a stream, returning an OptionalInt that contains the smallest value if present, or an empty OptionalInt if the stream is empty. This method is particularly useful for scenarios where you need to determine the smallest value in a sequence of integers. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications.

Comments