从本质上讲,我想一次性完成以下操作:
int sum = initialValue;
for (int n : collectionOfInts) {
sum += n;
}
return sum;
我看到有 http://functionaljava.org/examples/1.5/#Array.foldLeft ,但我宁愿不必复制该集合。
最佳答案
I see that there's http://functionaljava.org/examples/1.5/#Array.foldLeft, but I'd rather not have to copy the collection.
如果您使用 IterableW 中的 foldLeft|而不是数组,您无需复制任何内容。
关于java - C+ +'s accumulate or Groovy' 注入(inject)的 Java 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8496967/