avg value from Collection of numbers - Java java.util

Java examples for java.util:Collection Algorithm

Description

avg value from Collection of numbers

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {


    public static Float avg(Collection<Number> list) {
        Number sum = 0;/*ww  w  .j  av  a2  s. c  o  m*/
        for (Number item : list) {
            sum = sum.floatValue() + item.floatValue();
        }
        return sum.floatValue() / list.size();
    }
}

Related Tutorials