If there's no performance penalty incurred by holding the data set in memory and calculating this on the fly, this example might point you in the right direction;
public class DataSet {
private final List<Double> values = new ArrayList<Double>();
public void add(final double value) {
values.add(value);
}
public double getSum() {
double sum = 0;
for(double value : values)
sum += value;
}
public double getAverage() {
if (values.size() == 0)
return 0;
return getSum() / values.size();
}
public double getPopulationStandardDeviation() {
final double average = getAverage();
double sum = 0;
for(double value : values) {
final double v = (value - average);
sum += v * v;
}
return sum;
}
public double getStandardDeviation() {
if (values.size() == 0)
return 0;
return Math.sqrt(getPopulationStandardDeviation() / values.size());
}
}
Hope this helps,
Fredrik