Java List Average calculateAverage(List list)

Here you can find the source of calculateAverage(List list)

Description

Average of ints in array x.

License

Open Source License

Parameter

Parameter Description
marks a parameter

Declaration

public static Double calculateAverage(List<Double> list) 

Method Source Code

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

import java.util.List;

public class Main {
    /**//w w w  .  j  av a  2 s.c  o m
     * Average of ints in array x. Adapted from:
     * http://stackoverflow.com/questions/10791568/calculating-average-of-an-array-list
     * null values are ignored, like R mean(..., na.rm= TRUE). 
     * Returns Float.NaN if input list is empty or only nulls. You can check for Float.NaN
     * with Float.isNaN(x); 
     * @param marks
     * @return
     */
    public static Double calculateAverage(List<Double> list) {
        double sum = 0;
        long N = 0;
        if (!list.isEmpty()) {
            for (Double z : list) {
                if (z != null && !Double.isNaN(z)) {
                    sum += z;
                    N++;
                }
            }
            return (double) sum / N;
        }
        return Double.NaN;
    }
}

Related

  1. average(List collection)
  2. averageDouble(final List values)
  3. averageLog(List logList)
  4. averageValue(List oper)
  5. calculateAverage(final List durations)
  6. computeAverage(List aList)
  7. getAverage(int period, List timeline)
  8. getAverage(List list)
  9. getAverage(List l)