Stream.max() method in Java with Examples Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 : Java // 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 : Java // 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 : Java // 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 : Java // 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 Comment More infoAdvertise with us Next Article StrictMath max() Method in Java With Examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream Java-Stream interface +2 More Practice Tags : JavaMisc Similar Reads StrictMath max() Method in Java With Examples The java.lang.StrictMath.max() method returns the larger of two values. There are four variations of this method with different types of parameters passed. All of them are discussed below : The max(double num1, double num2)is the inbuilt method which is used to get the maximum of two given double va 6 min read Stream min() method in Java with Examples Stream.min() returns the minimum 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. min() is a terminal operation which combines stream elements and returns a summary result. So, min() is a spec 3 min read Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) 3 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read Optional stream() method in Java with examples The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream<T> stream() Paramete 2 min read Stream takeWhile() method in Java with examples The takeWhile(java.util.function.Predicate) method returns a stream of the remaining elements of this stream after taken the longest prefix of elements that match the given predicate if the stream is ordered else a stream of a subset of elements taken from this stream that match the given predicate. 3 min read Like