Example usage for java.util.stream DoubleStream average

List of usage examples for java.util.stream DoubleStream average

Introduction

In this page you can find the example usage for java.util.stream DoubleStream average.

Prototype

OptionalDouble average();

Source Link

Document

Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty.

Usage

From source file:Main.java

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 {// w  w  w  . j a  va  2 s.  c om
        System.out.println("no value");
    }
}

From source file:com.hortonworks.streamline.streams.metrics.storm.ambari.AmbariMetricsServiceWithStormQuerier.java

private Map<Long, Double> aggregateStreamsForMetricsValues(Map<Long, List<Pair<String, Double>>> ret,
        AggregateFunction aggrFunction) {
    return ret.entrySet().stream().collect(toMap(e -> e.getKey(), e -> {
        DoubleStream valueStream = e.getValue().stream().mapToDouble(d -> d.getRight());
        switch (aggrFunction) {
        case SUM:
            return valueStream.sum();

        case AVG:
            return valueStream.average().orElse(0.0d);

        case MAX:
            return valueStream.max().orElse(0.0d);

        case MIN:
            return valueStream.min().orElse(0.0d);

        default:/*  w w  w  . java 2 s  . co m*/
            throw new IllegalArgumentException("Not supported aggregated function.");

        }
    }));
}