Given a list of integers, find the maximum value using streams.

max() takes a Comparator and returns the largest element as an Optional, because the stream could be empty. For a list of Integers, you can pass Integer::compare or Comparator.naturalOrder().

Key Points

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FindMaxElement {

    public static void main(String[] args) {

        List<Integer> list = Arrays.asList(10, 15, 8, 49, 25, 98, 98, 32, 15);

        int max = list.stream()
                      .max(Comparator.naturalOrder())
                      .orElseThrow();
        System.out.println("Max: " + max);

        // Primitive stream version (no boxing)
        int max2 = list.stream()
                       .mapToInt(Integer::intValue)
                       .max()
                       .getAsInt();
        System.out.println("Max: " + max2);

        int min = list.stream().min(Integer::compare).orElseThrow();
        System.out.println("Min: " + min);
    }
}

Output

Max: 98

Max: 98

Min: 8

Good to Know

A frequent follow-up is "Find the second-highest number". Use list.stream().distinct().sorted(Comparator.reverseOrder()).skip(1).findFirst().

Interview Tip

A concise interview answer is

I use stream().max(Comparator.naturalOrder()), which returns an Optional, and read it with orElseThrow. To avoid boxing I can use mapToInt(Integer::intValue).max(), and min() works the same way for the smallest value.

Last updated: 11 November 2026

Previous question: Given a list of integers, count the elements using streams.

Next question: Given a string, find the first non-repeated character using streams.

More Stream API Coding Questions - Level II interview questions

All Stream API Coding Questions - Level II questions