Java Collection Mean mean(Collection vector)

Here you can find the source of mean(Collection vector)

Description

mean

License

Open Source License

Declaration

public staticdouble mean(Collection<Number> vector) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**//from  w  w  w  .  j a va 2 s. c  o  m
     * Computes the mean for an array of Numbers.
     * 
     * @param vector
     *          the array
     * @return the mean
     */
    public static/* @pure@ */double mean(Number[] vector) {

        double sum = 0;

        if (vector.length == 0) {
            return 0;
        }
        for (int i = 0; i < vector.length; i++) {
            sum += vector[i].doubleValue();
        }
        return sum / (double) vector.length;
    }

    public static/* @pure@ */double mean(Collection<Number> vector) {
        return mean(vector.toArray(new Number[vector.size()]));
    }

    public static/* @pure@ */double mean(double[] vector) {

        double sum = 0;

        if (vector.length == 0) {
            return 0;
        }
        for (int i = 0; i < vector.length; i++) {
            sum += vector[i];
        }
        return sum / (double) vector.length;
    }
}

Related

  1. getVariance(Collection data, Number mean)
  2. mean(Collection coll)
  3. mean(Collection nums)
  4. mean(Collection data)
  5. mean(Collection dist)
  6. mean(final Collection values)
  7. mean(final Collection values)
  8. meanAndVariance(Collection vector)