Java Collection Mean calculateMean(Collection values)

Here you can find the source of calculateMean(Collection values)

Description

calculate Mean

License

Apache License

Declaration

public static double calculateMean(Collection<Double> values) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

public class Main {
    public static double calculateMean(Collection<Double> values) {
        if (values == null || values.isEmpty()) {
            throw new IllegalArgumentException("Can not calculate mean of null or empty collection!");
        }/*ww w  .  j ava  2s .  co m*/

        return (calcSum(values) / values.size());

    }

    public static double calculateMean(double[] values) {
        if (values == null) {
            throw new IllegalArgumentException("Can not calculate mean of null!");
        }

        return (calcSum(values) / values.length);

    }

    public static double calcSum(Collection<Double> values) {
        if (values == null) {
            throw new IllegalArgumentException("Can not calculate sum of null!");
        }
        double sum = 0D;
        for (double d : values) {
            sum += d;
        }
        return sum;
    }

    public static double calcSum(double[] values) {
        if (values == null) {
            throw new IllegalArgumentException("Can not calculate sum of null!");
        }
        double sum = 0D;
        for (double d : values) {
            sum += d;
        }
        return sum;
    }
}

Related

  1. calculateMean(Collection values)
  2. calculateMean(Collection values)
  3. getMean(Collection data)
  4. getMean(Collection cluster)
  5. getMean(final Collection collection)
  6. getVariance(Collection data, Number mean)