VOOZH about

URL: https://www.geeksforgeeks.org/java/intstream-average-in-java-with-examples/

⇱ IntStream average() in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

IntStream average() in Java with Examples

Last Updated : 14 Sep, 2021

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 :  

Output : 

5.0

Example 2 :  

Output : 

4.857142857142857


 

Comment