# The Java Embedded Aggregator Class VarAggregate

VarAggregate implements the variance aggregation.

### For an overview see page Java Aggregator Class

### Class Definition

        public static class VarAggregate implements Aggregate<Number>
        {
            public void initialize(Number val)
            {
                double v = val.doubleValue();
                sum = v;
                sum2 = v*v;
                count = 1;
            }

            public void accumulate(Number val)
            {
                double v = val.doubleValue();
                sum += v;
                sum2 += v*v;
                count += 1;
            }

            public Object result()
            {
                return new Double((sum2 - sum*sum/count)/count);
            }

            public void merge(Aggregate<Number> other)
            {
                VarAggregate otherVar = (VarAggregate)other;
                sum += otherVar.sum;
                sum2 += otherVar.sum2;
                count += otherVar.count;
            }

            double sum;
            double sum2;
            long count;
        }