Stream.max() returns the maximum element of the stream based on the provided Comparator. A Comparator is a comparison function, which imposes a total ordering on some collection of objects. max() is a terminal operation which combines stream elements and returns a summary result. So, max() is a special case of reduction. The method returns Optional instance.
Syntax :
Optional<T> max(Comparator<? super T> comparator) Where, Optional is a container object which may or may not contain a non-null value and T is the type of objects that may be compared by this comparator
Exception : This method throws NullPointerException if the maximum element is null.
Example 1 :
// Implementation of Stream.max() // to get the maximum element // of the Stream according to the // provided Comparator. import java.util.*; import java.util.Optional; import java.util.Comparator; class GFG { // Driver code public static void main(String[] args) { // Creating a list of integers List<Integer> list = Arrays.asList(-9, -18, 0, 25, 4); System.out.print("The maximum value is : "); // Using stream.max() to get maximum // element according to provided Comparator // and storing in variable var Integer var = list.stream().max(Integer::compare).get(); System.out.print(var); } } |
Output :
The maximum value is : 25
Example 2 :
// Implementation of Stream.max() // to get the maximum element // of the Stream according to the // provided Comparator. import java.util.*; import java.util.Optional; import java.util.Comparator; class GFG { // Driver code public static void main(String[] args) { // Creating a list of integers List<Integer> list = Arrays.asList(-9, -18, 0, 25, 4); // Using stream.max() to get maximum // element according to provided Comparator // Here, the smallest element in list // will be stored in variable var Optional<Integer> var = list.stream() .max(Comparator.reverseOrder()); // If a value is present, isPresent() // will return true, else display message if (var.isPresent()) { System.out.println(var.get()); } else { System.out.println("-1"); } } } |
Output :
-18
Example 3 :
// Implementation of Stream.max() // to get the maximum element // of the Stream according to the // provided Comparator. import java.util.*; import java.util.Optional; import java.util.Comparator; class GFG { // Driver code public static void main(String[] args) { // Creating a list of Strings List<String> list = Arrays.asList("G", "E", "E", "K", "g", "e", "e", "k"); // using Stream.max() method with Comparator // Here, the character with maximum ASCII value // is stored in variable MAX String MAX = list.stream().max(Comparator. comparing(String::valueOf)).get(); // Displaying the maximum element in // the stream according to provided Comparator System.out.println("Maximum element in the " + "stream is : " + MAX); } } |
Output :
Maximum element in the stream is : k
Example 4 :
// Implementation of Stream.max() // to get the maximum element // of the Stream according to the // provided Comparator. import java.util.*; import java.util.Optional; import java.util.Comparator; class GFG { // Driver code public static void main(String[] args) { // creating an array of strings String[] array = { "Geeks", "for", "GeeksforGeeks", "GeeksQuiz" }; // Here, the Comparator compares the strings // based on their last characters and returns // the maximum value accordingly // The result is stored in variable MAX Optional<String> MAX = Arrays.stream(array).max((str1, str2) -> Character.compare(str1.charAt(str1.length() - 1), str2.charAt(str2.length() - 1))); // If a value is present, // isPresent() will return true if (MAX.isPresent()) System.out.println(MAX.get()); else System.out.println("-1"); } } |
Output :
GeeksQuiz
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

