IntStream max() in Java with examples Last Updated : 06 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report java.util.stream.IntStream in Java 8, deals with primitive ints. 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. IntStream max() returns an OptionalInt describing the maximum element of this stream, or an empty optional if this stream is empty. Syntax : OptionalInt() max() Where, OptionalInt is a container object which may or may not contain a int value. Example 1 : Java // Java code for IntStream max() import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream IntStream stream = IntStream.of(-9, -18, 54, 8, 7, 14, 3); // OptionalInt is a container object // which may or may not contain a // int value. OptionalInt obj = stream.max(); // If a value is present, isPresent() will // return true and getAsInt() will // return the value if (obj.isPresent()) { System.out.println(obj.getAsInt()); } else { System.out.println("-1"); } } } Output : 54 Example 2 : Java // Java code for IntStream max() // to get the maximum value in range // excluding the last element import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // To find maximum in given range IntStream stream = IntStream.range(50, 75); // storing the maximum value in variable // if it is present, else show -1. int maximum = stream.max().orElse(-1); // displaying the maximum value System.out.println(maximum); } } Output : 74 Example 3 : Java // Java code for IntStream max() // to get the maximum value in range // excluding the last element import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // To find maximum in given range IntStream stream = IntStream.range(50, 50); // storing the maximum value in variable // if it is present, else show -1. int maximum = stream.max().orElse(-1); // displaying the maximum value System.out.println(maximum); } } Output : -1 Comment More infoAdvertise with us Next Article IntStream max() in Java with examples S Sahil_Bansall Follow Improve Article Tags : Misc Java Java - util package Java-Functions java-stream java-intstream +2 More Practice Tags : JavaMisc Similar Reads IntStream min() in Java with Examples java.util.stream.IntStream in Java 8, deals with primitive ints. It helps to solve the old 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. IntStream min() returns an OptionalInt describing th 2 min read Stream.max() method in Java with Examples 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 spec 3 min read IntSummaryStatistics getMax() method in Java with Examples The getMax() method of IntSummaryStatistics class in Java is used to get the maximum of records in this IntSummaryStatistics. Syntax: public int getMax() Parameter: This method do not accept any value as parameter. Return Value: This method returns the maximum of the records in this IntSummaryStatis 1 min read IntSummaryStatistics getMin() method in Java with Examples The getMin() method of IntSummaryStatistics class in Java is used to get the minimum of records in this IntSummaryStatistics. Syntax: public int getMin() Parameter: This method do not accept any value as parameter. Return Value: This method returns the minimum of the records in this IntSummaryStatis 1 min read BigInteger intValueExact() Method in Java with Examples java.math.BigInteger.intValueExact() was introduced in Java 8. It is an inbuilt function which converts the value of BigInteger to a int and checks for lost information. If the value of BigInteger is greater than 2,147,483,647 or less than -2,147,483,648; the method will throw ArithmeticException as 3 min read Like