How do you find the maximum value in a list using Java streams?

max() is a terminal Stream operation that returns the largest element in the stream, according to a supplied Comparator, wrapped in an Optional.

Key Points

Example

Given the list [4, 9, 2, 7], calling max(Integer::compare) returns an Optional containing 9, the largest value.

Optional<Integer> max = numbers.stream()
        .max(Integer::compare);

Interview Tip

A concise interview answer is

max(Comparator) scans the whole stream and returns the largest element wrapped in an Optional, so I don't have to handle an empty stream with a null check. For primitive streams like IntStream, max() returns an OptionalInt directly with no comparator needed.

Last updated: 11 November 2026

Previous question: How do you filter even numbers from a list using Java streams?

Next question: How do you find the sum of list elements using Java streams?

More Stream API Coding Questions - Level I interview questions

All Stream API Coding Questions - Level I questions