Java Stream max() Method

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

Table of Contents

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

Introduction

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

max() Method Syntax

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

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

Parameters:

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

Returns:

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

Throws:

  • This method does not throw any exceptions.

Understanding max()

The max() method allows you to find the maximum 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 max(), we will create a Stream of integers and use max() to find the maximum value.

Example

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

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

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

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

Output:

5

Using max() with Custom Comparator

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

Example

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

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

        // Use max() to find the longest string
        Optional<String> longestString = stream.max(Comparator.comparingInt(String::length));

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

Output:

banana

Real-World Use Case

Finding the Employee with the Highest Salary

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

Example

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

public class MaxSalaryExample {
    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 max() to find the employee with the highest salary
        Optional<Employee> highestPaidEmployee = employees.max(Comparator.comparingInt(Employee::getSalary));

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

Output:

Charlie: 70000

Conclusion

The Stream.max() method is used to find the maximum element of the stream according to the provided Comparator. This method is particularly useful for determining the largest 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 maximum elements as needed.

Comments