Java Collection Mean calculateMean(Collection values)

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

Description

Returns the mean of a collection of Number objects.

License

Open Source License

Parameter

Parameter Description
values Collection of values; elements may not be null ; NaN and infinite values will be ignored

Return

The mean of the given

Declaration

public static double calculateMean(Collection<? extends Number> values) 

Method Source Code

//package com.java2s;
/**/* w  w w.ja va 2  s  . com*/
 * This file is part of SADL, a library for learning all sorts of (timed) automata and performing sequence-based anomaly detection.
 * Copyright (C) 2013-2016  the original author or authors.
 *
 * SADL is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * SADL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with SADL.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Returns the mean of a collection of {@link Number} objects.
     * 
     * @param values
     *            {@link Collection} of values; elements may not be {@code null}; {@code NaN} and infinite values will be ignored
     * 
     * @return The mean of the given {@link Collection}
     */
    public static double calculateMean(Collection<? extends Number> values) {

        int count = 0;
        double total = 0.0;
        final Iterator<? extends Number> iterator = values.iterator();
        while (iterator.hasNext()) {
            final double value = iterator.next().doubleValue();
            if (!Double.isNaN(value) && !Double.isInfinite(value)) {
                total += value;
                count++;
            }
        }
        return total / count;
    }
}

Related

  1. calculateMean(Collection values)
  2. calculateMean(Collection values)
  3. getMean(Collection data)
  4. getMean(Collection cluster)