IntStream average() example

Description

IntStream average() returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty.

Syntax

average has the following syntax.


OptionalDouble average()

Example

The following example shows how to use average.


import java.util.OptionalDouble;
import java.util.stream.IntStream;
//  ww w.j  a v  a  2  s.  c o  m
public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    OptionalDouble d = i.average();
    if(d.isPresent()){
      System.out.println(d.getAsDouble());
    }else{
      System.out.println("no value");  
    }    
  }
}

The code above generates the following result.





















Home »
  Java Streams »
    Reference »




Collectors
DoubleStream
DoubleStream.Builder
IntStream
IntStream.Builder
LongStream
LongStream.Builder
Stream
Stream.Builder