IntStream average() in Java with Examples Last Updated : 14 Sep, 2021 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 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 average() returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. Syntax : OptionalDouble average() Where, OptionalDouble is a container object which may or may not contain a double value. Below given are some examples to understand the function in a better way. Example 1 : Java // Java code for IntStream average() 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(2, 3, 4, 5, 6, 7, 8); // OptionalDouble is a container object // which may or may not contain a // double value. OptionalDouble obj = stream.average(); // If a value is present, isPresent() will // return true and getAsDouble() will // return the value if (obj.isPresent()) { System.out.println(obj.getAsDouble()); } else { System.out.println("-1"); } } } Output : 5.0 Example 2 : Java // Implementation of IntStream average() 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(2, 3, 3, 4, 6, 8, 8); // OptionalDouble is a container object // which may or may not contain a // double value. OptionalDouble obj = stream.average(); // If a value is present, isPresent() will // return true and getAsDouble() will // return the value if (obj.isPresent()) { System.out.println(obj.getAsDouble()); } else { System.out.println("-1"); } } } Output : 4.857142857142857 Comment More infoAdvertise with us Next Article Java | Collectors averagingDouble() 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 LongStream average() in Java with Examples java.util.stream.LongStream in Java 8, deals with primitive Longs. 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. LongStream average() returns an OptionalDouble des 2 min read IntSummaryStatistics accept() method in Java with Examples The accept() method of IntSummaryStatistics class in Java is used to accept the given value into this summary information. Syntax: public void accept(int value) Parameter: This method accepts value as parameter that is to be recorded into this IntSummaryStatistics. Return Value: This method do not r 1 min read Java 8 | Collectors averagingInt() with Examples Collectors averagingInt(ToIntFunction<? super T> mapper) method is used to find the mean of the integers passed in the parameters. This method returns a Collector that produces the arithmetic mean of an integer-valued function applied to the input elements. If no elements are passed as the inp 3 min read Java | Collectors averagingDouble() with Examples Collectors averagingDouble(ToDoubleFunction<? super T> mapper) method is used to find the mean of the double values passed in the parameters. This method returns a Collector that produces the arithmetic mean of an double-valued function applied to the input elements. If no elements are passed 3 min read Java.util.IntSummaryStatistics class with Examples The IntSummaryStatistics class is present in java.util package. It takes a collection of Integer objects and is useful in the circumstances when we are dealing with a stream of integers. It maintains a count of the number of integers it has processed, their sum and various other statistics. The clas 3 min read IntSummaryStatistics getSum() method in Java with Examples The getSum() method of IntSummaryStatistics class in Java is used to get the sum of records in this IntSummaryStatistics. Syntax: public long getSum() Parameter: This method do not accept any value as parameter. Return Value: This method returns the sum of the records in this IntSummaryStatistics. P 1 min read Like