LongStream max() in Java with examples
java.util.stream.LongStream in Java 8, deals with primitive longs. It helps to solve the problems like finding maximum value in array, finding minimum value in array, sum of all elements in array, and average of all values in array in a new way. LongStream max() returns an OptionalLong describing the maximum element of this stream, or an empty optional if this stream is empty.
Syntax :
OptionalLong() max() Where, OptionalLong is a container object which may or may not contain a long value.
Example 1 :
// Java code for LongStream max() import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream LongStream stream = LongStream.of(-9L, -18L, 54L, 8L, 7L, 14L, 3L); // OptionalLong is a container object // which may or may not contain a // long value. OptionalLong obj = stream.max(); // If a value is present, isPresent() will // return true and getAsLong() will // return the value if (obj.isPresent()) { System.out.println(obj.getAsLong()); } else { System.out.println("-1"); } } } |
Output :
54
Example 2 :
// Java code for LongStream max() // to get the maximum value in range // excluding the last element import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // To find maximum in given range LongStream stream = LongStream.range(50L, 75L); // storing the maximum value in variable // if it is present, else show -1. long maximum = stream.max().orElse(-1); // displaying the maximum value System.out.println(maximum); } } |
Output :
74
Example 3 :
// Java code for LongStream max() // to get the maximum value in range // excluding the last element import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // To find maximum in given range LongStream stream = LongStream.range(50L, 50L); // storing the maximum value in variable // if it is present, else show -1. long maximum = stream.max().orElse(-1); // displaying the maximum value System.out.println(maximum); } } |
Output :
-1
Recommended Posts:
- LongStream min() in Java with examples
- LongStream empty() in Java with examples
- LongStream average() in Java with Examples
- LongStream distinct() in Java with examples
- LongStream filter() in Java with examples
- LongStream count() in Java with examples
- LongStream noneMatch() in Java with examples
- LongStream allMatch() in Java with examples
- LongStream mapToInt() in Java with Examples
- LongStream peek() in Java with examples
- LongStream anyMatch() in Java with examples
- LongStream reduce(long identity, LongBinaryOperator op) in Java with Examples
- LongStream findAny() with examples
- LongStream sum() in Java
- LongStream of() in Java
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.



