Find maximum element from an Integer List using Stream in Java
This post will discuss how to find the maximum element from a list of Integer using stream in Java.
1. Converting to IntStream
The idea is to convert the list to IntStream and use the max() method provided by IntStream that returns an OptionalInt containing the maximum element of the stream. To unwrap an OptionalInt and get a hold of real Integer inside, we can either use orElseThrow or orElse or orElseGet.
|
1 2 3 4 5 6 7 8 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() // Stream<Integer> .mapToInt(v -> v) // IntStream .max() // OptionalInt .orElse(Integer.MIN_VALUE); // Integer } |
2. Using Stream.max() method
The idea is to convert the list into a Stream and call Stream#max() that accepts a Comparator to compare items in the stream against each other to find the maximum element, and returns an Optional containing the maximum element in the stream according to the provided Comparator.
|
1 2 3 4 5 6 7 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() // Stream<Integer> .max(Comparator.naturalOrder()) // Optional<Integer> .get(); // Integer } |
Since Comparator is a functional interface, and Integer.compare() takes two ints as an argument and returns an int value similar to a Comparator#compare(), it complies with that interface.
|
1 2 3 4 5 6 7 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() // Stream<Integer> .max(Integer::compare) // Optional<Integer> .get(); // Integer } |
3. Reduction operation
We can also perform a reduction operation on the stream elements using the Integer#max() method, which then returns an Optional describing the reduced value, which will be the maximum value present.
|
1 2 3 4 5 6 7 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() // Stream<Integer> .reduce(Integer::max) // Optional<Integer> .get(); // Integer } |
We can also use the overloaded version of the reduce() method, which performs a reduction on the stream elements, using the provided identity value and an associative accumulation function, and returns the reduced value.
|
1 2 3 4 5 6 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() .reduce(Integer.MIN_VALUE, Integer::max); } |
4. Using Collectors
We can also use Collectors to find the maximum element in the list.
1. Collectors#maxBy() returns a Collector that produces the maximal element according to a given Comparator.
|
1 2 3 4 5 6 7 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() .collect(Collectors.maxBy(Comparator.naturalOrder())) .get(); } |
2. Collectors#summarizingInt() returns a Collector, which applies an int-producing mapping method to each input element and returns summary statistics for the resulting values.
The returned IntSummaryStatistics object contains statistics such as count, min, max, sum, and average.
|
1 2 3 4 5 6 7 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() .collect(Collectors.summarizingInt(Integer::intValue)) .getMax(); } |
3. Collectors#reducing() returns a Collector which performs a reduction of its input elements under a specified BinaryOperator and returns an Optional.
|
1 2 3 4 5 6 7 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() .collect(Collectors.reducing(Integer::max)) .get(); } |
5. Using Sorting
This is the simplest but most inefficient way. The idea is to sort the stream in the reverse order, then the first element of the stream would be the maximum element.
|
1 2 3 4 5 6 7 8 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() .sorted(Comparator.<Integer>reverseOrder()) .findFirst() .get(); } |
To avoid NullPointerException if the given list is null and NoSuchElementException if the given list is empty, we can add the following code to all the above-mentioned methods at the beginning:
|
1 2 3 |
if (list == null || list.size() == 0) { return Integer.MIN_VALUE; } |
That’s all about finding the maximum element from an Integer List using Stream in Java.
References:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)