DoubleStream average() in Java with Examples

java.util.stream.DoubleStream in Java 8, deals with primitive doubles. 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. DoubleStream average() returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. If any recorded value is a NaN or the sum is at any point a NaN then the average will be NaN.
Syntax :

OptionalDouble average()

Where, OptionalDouble is a container object 
which may or may not contain a double value.

Note : The average returned can vary depending upon the order in which values are recorded. Elements sorted by increasing absolute magnitude tend to yield more accurate results.

Example 1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for DoubleStream average()
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream
        DoubleStream stream = DoubleStream.of(2.5, 3.6, 4.7, 5.0, 6.2);
  
        // 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");
        }
    }
}

chevron_right


Output:

4.4

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for DoubleStream average()
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // OptionalDouble is a container object
        // which may or may not contain a
        // double value.
        OptionalDouble obj = DoubleStream.empty().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");
        }
    }
}

chevron_right


Output:

-1

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for DoubleStream average()
import java.util.*;
import java.util.stream.DoubleStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // creating a stream
        DoubleStream stream = DoubleStream.of(2.5, 3.6, 4.7,
                                              Double.MAX_VALUE, Double.MAX_VALUE);
  
        // 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");
        }
    }
}

chevron_right


Output:

Infinity


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

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.