Java tutorial
//package com.java2s; import java.util.Iterator; import java.util.function.Function; public class Main { public static <T> int sum(Iterable<T> source, Function<T, Integer> func) { // TODO Auto-generated method stub if (source == null || !source.iterator().hasNext()) return 0; Iterator<T> it = source.iterator(); int count = 0; for (T item : source) { count += func.apply(item); } /*for (T item = it.next(); it.hasNext();it.) { count += func.apply(item); }*/ return count; } }