There are basically two useful libraries that can help with this; Google Guava and Apache Commons Collections.
What you are trying to do is basically two operations, first mapping, then reduction. I've never used Commons Collections to any extent myself so I can't tell you more about that, but I know there is no support for reduction (or folding) in Google Guava at least (see Issue 218). This is not too hard to add yourself though (not tested):
interface Function2<A, B> {
B apply(B b, A a);
}
public class Iterables2 {
public static <A, B> B reduce(Iterable<A> iterable,
B initial, Function2<A, B> fun) {
B b = initial;
for (A item : iterable)
b = fun.apply(b, item);
return b;
}
}
That way you can combine it with Guavas Iterables.transform() like so:
class Summer implements Function2<Integer, Integer> {
Integer apply(Integer b, Integer a) {
return b + a;
}
}
class MyMapper<T> implements Function<T, Integer> {
Integer apply(T t) {
// Do stuff
}
}
And then (provided you've import static'ed the relevant classes):
reduce(transform(iterable, new MyMapper()), 0, new Summer());
Also see this question.