实施正确的方法并不困难foldLeft
对于 Java 8 流:
@SuppressWarnings("unchecked")
public static <T, U> U foldLeft(Stream<T> stream,
U identity, BiFunction<U, ? super T, U> accumulator) {
Object[] result = new Object[] { identity };
stream.forEachOrdered(t -> result[0] = accumulator.apply((U) result[0], t));
return (U) result[0];
}
或者以类型安全的方式:
public static <T, U> U foldLeft(Stream<T> stream,
U identity, BiFunction<U, ? super T, U> accumulator) {
class Box {
U value;
Box(U value) { this.value = value; }
}
Box result = new Box(identity);
stream.forEachOrdered(t -> result.value = accumulator.apply(result.value, t));
return result.value;
}
这适用于顺序流和并行流。如果您的流具有一些消耗 CPU 的无状态中间操作,例如map
:在这种情况下,可以通过以下方式处理下一个元素map
与当前元素并行处理foldLeft
。我不同意这样的操作不适合Stream API,因为它可以通过已经存在的正确表达forEachOrdered
.
我有这个操作StreamEx https://github.com/amaembo/streamex库,所以你可以像这样使用它:
WebTarget target = EntryStream.of(queryParams).foldLeft(getClient().target(u),
(t, entry) -> t.queryParam(entry.getKey(), entry.getValue()))