sum value from Collection - Java java.util

Java examples for java.util:Collection Algorithm

Description

sum value from Collection

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        Collection list = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(sum(list));
    }/*ww w.  j a  va2s  .c  o  m*/

    public static Number sum(Collection<Number> list) {
        Number sum = 0;
        for (Number item : list) {
            sum = sum.floatValue() + item.floatValue();
        }
        return sum;
    }
}

Related Tutorials