Java Collection Average average(Collection values)

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

Description

average

License

Open Source License

Declaration

public static Double average(Collection<Double> values) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Collection;

import java.util.List;

public class Main {
    public static Double average(Collection<Double> values) {
        List<Double> filteredValues = filterNulls(values);
        if (filteredValues.size() == 0)
            return null;

        double sum = 0;
        for (Double value : filteredValues) {
            sum += value;//w w w.j a v a  2  s  . c  om
        }

        return sum / filteredValues.size();
    }

    private static List<Double> filterNulls(Collection<Double> values) {
        List<Double> result = new ArrayList<>();
        for (Double value : values) {
            if (value != null)
                result.add(value);
        }

        return result;
    }
}

Related

  1. average(Collection a)
  2. average(Collection col)
  3. average(Collection sizes)
  4. average(Collection values)
  5. average(Collection values)
  6. average(Collection values)