DoubleStream average()
returns an OptionalDouble describing the arithmetic mean of
elements of this stream, or an empty optional if this stream is empty.
average
has the following syntax.
OptionalDouble average()
The following example shows how to use average
.
import java.util.OptionalDouble; import java.util.stream.DoubleStream; // w w w . ja va2 s . c o m public class Main { public static void main(String[] args) { DoubleStream d = DoubleStream.of(1.2,2.3,4.5); OptionalDouble v = d.average(); if(v.isPresent()){ System.out.println(v.getAsDouble()); }else{ System.out.println("no value"); } } }
The code above generates the following result.