Java Stream min() Method

The min() method in Java, part of the java.util.stream.Stream interface, is used to find the minimum element of the stream according to the provided Comparator. This method is useful when you need to determine the smallest element in a stream based on a specific comparison criterion.

Table of Contents

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

Introduction

The min() method returns an Optional<T> describing the minimum element of the stream according to the provided Comparator. This method is a terminal operation, meaning it consumes the stream and returns a result.

min() Method Syntax

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

Optional<T> min(Comparator<? super T> comparator)

Parameters:

  • comparator: A Comparator to compare elements of the stream.

Returns:

  • An Optional<T> describing the minimum element of the stream, or an empty Optional if the stream is empty.

Throws:

  • This method does not throw any exceptions.

Understanding min()

The min() method allows you to find the minimum element of the stream based on the provided Comparator. If the stream is empty, the result is an empty Optional.

Examples

Basic Usage

To demonstrate the basic usage of min(), we will create a Stream of integers and use min() to find the minimum value.

Example

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

public class MinExample {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(5, 2, 8, 3, 1);

        // Use min() to find the minimum value
        Optional<Integer> min = stream.min(Comparator.naturalOrder());

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

Output:

1

Using min() with Custom Comparator

This example shows how to use min() with a custom Comparator to find the shortest string in a stream.

Example

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

public class MinWithComparatorExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("apple", "banana", "cherry", "date");

        // Use min() to find the shortest string
        Optional<String> shortestString = stream.min(Comparator.comparingInt(String::length));

        // Print the shortest string if present
        shortestString.ifPresent(System.out::println);
    }
}

Output:

date

Real-World Use Case

Finding the Employee with the Lowest Salary

In real-world applications, the min() method can be used to find the employee with the lowest salary from a stream of employees.

Example

import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Stream;

public class MinSalaryExample {
    static class Employee {
        String name;
        int salary;

        Employee(String name, int salary) {
            this.name = name;
            this.salary = salary;
        }

        int getSalary() {
            return salary;
        }

        @Override
        public String toString() {
            return name + ": " + salary;
        }
    }

    public static void main(String[] args) {
        Stream<Employee> employees = Stream.of(
            new Employee("Alice", 50000),
            new Employee("Bob", 60000),
            new Employee("Charlie", 70000)
        );

        // Use min() to find the employee with the lowest salary
        Optional<Employee> lowestPaidEmployee = employees.min(Comparator.comparingInt(Employee::getSalary));

        // Print the employee with the lowest salary if present
        lowestPaidEmployee.ifPresent(System.out::println);
    }
}

Output:

Alice: 50000

Conclusion

The Stream.min() method is used to find the minimum element of the stream according to the provided Comparator. This method is particularly useful for determining the smallest element in a stream based on a specific comparison criterion. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, finding the minimum elements as needed.

Comments