Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

DoubleSummaryStatistics — statistics for double values

DoubleSummaryStatistics collects the count, sum, minimum, maximum, and average of double values. accept adds a value, while combine merges statistics.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
stats.accept(value);
stats.combine(other);
stats.getCount();
stats.getSum();
stats.getMin();
stats.getMax();
stats.getAverage();

java.util.DoubleSummaryStatistics is a mutable accumulator. accept(double) records a value; combine(other) merges statistics into the current object.

getCount() returns long; getSum(), getMin(), getMax(), and getAverage() return double. An empty object has zero count, sum, and average, minimum Double.POSITIVE_INFINITY, and maximum Double.NEGATIVE_INFINITY.

NaN makes the minimum, maximum, sum, and average NaN. Sum and average are subject to floating-point error; summation order is unspecified.

The object is not thread-safe: do not update a shared instance concurrently without synchronization.

Examples

import java.util.DoubleSummaryStatistics;

public class Main {
    public static void main(String[] args) {
        DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
        stats.accept(2.0);
        stats.accept(4.0);

        DoubleSummaryStatistics other = new DoubleSummaryStatistics();
        other.accept(6.0);
        stats.combine(other);

        System.out.println("count=" + stats.getCount());
        System.out.println("sum=" + stats.getSum());
        System.out.println("min=" + stats.getMin());
        System.out.println("max=" + stats.getMax());
        System.out.println("average=" + stats.getAverage());
    }
}

Result

count=3
sum=12.0
min=2.0
max=6.0
average=4.0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources