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.
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 :
The maximum value is : 25
Example 2 :
Example 3 :
Maximum element in the stream is : k
Example 4 :
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.
- 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 firstname.lastname@example.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.