< prev index next >

src/java.base/share/classes/java/util/stream/IntStream.java

Print this page

        

*** 541,550 **** --- 541,691 ---- * @see #reduce(int, IntBinaryOperator) */ OptionalInt reduce(IntBinaryOperator op); /** + * Folds the elements of this stream using the provided {@code seed} value and + * accumulation function, going left to right. This is equivalent to: + * + * <pre>{@code + * int result = seed; + * for (int element : this stream) + * result = accumulator.apply(result, element) + * return result; + * } + * </pre> + * + * <p>This is a <a href="package-summary.html#StreamOps">terminal + * operation</a>. + * + * <p>This method cannot take all the advantages of parallel streams as it must + * process elements strictly left to right. If your accumulator function + * is associative, and {@code seed} value has identity properties, + * consider using {@link #reduce(int, IntBinaryOperator)} method. + * + * @param seed the starting value + * @param accumulator a <a href="package-summary.html#NonInterference">non-interfering</a>, + * <a href="package-summary.html#Statelessness">stateless</a> + * function for incorporating an additional element into a result + * @return the result of the folding + * @see #reduce(int, IntBinaryOperator) + * @see #foldLeft(IntBinaryOperator) + * @since 10 + */ + default int foldLeft(int seed, IntBinaryOperator accumulator) { + ReduceOps.IntFoldLeftOp op = new ReduceOps.IntFoldLeftOp(accumulator); + op.accept(seed); + forEachOrdered(op); + return op.getAsInt(); + } + + /** + * Folds the elements of this stream using the provided accumulation + * function, going left to right. This is equivalent to: + * + * <pre>{@code + * boolean foundAny = false; + * int result = 0; + * for (int element : this stream) { + * if (!foundAny) { + * foundAny = true; + * result = element; + * } + * else + * result = accumulator.apply(result, element); + * } + * return foundAny ? OptionalInt.of(result) : OptionalInt.empty(); + * } + * </pre> + * + * <p>This is a <a href="package-summary.html#StreamOps">terminal + * operation</a>. + * + * <p>This method cannot take all the advantages of parallel streams as it must + * process elements strictly left to right. If your accumulator function is + * associative, consider using {@link #reduce(IntBinaryOperator)} method. + * + * @param accumulator a <a href="package-summary.html#NonInterference">non-interfering</a>, + * <a href="package-summary.html#Statelessness">stateless</a> + * function for incorporating an additional element into a result + * @return the result of the folding + * @see #foldLeft(int, IntBinaryOperator) + * @see #reduce(IntBinaryOperator) + * @since 10 + */ + default OptionalInt foldLeft(IntBinaryOperator accumulator) { + ReduceOps.IntFoldLeftOp op = new ReduceOps.IntFoldLeftOp(accumulator); + forEachOrdered(op); + return op.get(); + } + + /** + * Folds the elements of this stream using the provided {@code seed} value and + * accumulation function, going right to left. + * + * <p>This is a <a href="package-summary.html#StreamOps">terminal + * operation</a>. + * + * <p>As this method must process elements strictly right to left, it cannot + * start processing till all the previous stream stages complete. Also it + * requires intermediate memory to store the whole content of the stream as + * the stream natural order is left to right. If your accumulator function + * is associative, and {@code seed} value has identity properties, + * consider using {@link #reduce(int, IntBinaryOperator)} method. + * + * @param seed the starting value + * @param accumulator a <a href="package-summary.html#NonInterference">non-interfering</a>, + * <a href="package-summary.html#Statelessness">stateless</a> + * function for incorporating an additional element into a result + * @return the result of the folding + * @see #foldLeft(int, IntBinaryOperator) + * @see #reduce(int, IntBinaryOperator) + * @since 10 + */ + default int foldRight(int seed, IntBinaryOperator accumulator) { + int[] array = toArray(); + int result = seed; + for(int idx = array.length - 1; idx >= 0; idx--) { + result = accumulator.applyAsInt(array[idx], result); + } + return result; + } + + /** + * Folds the elements of this stream using the provided accumulation + * function, going right to left. + * + * <p>This is a <a href="package-summary.html#StreamOps">terminal + * operation</a>. + * + * <p>As this method must process elements strictly right to left, it cannot + * start processing till all the previous stream stages complete. Also it + * requires intermediate memory to store the whole content of the stream as + * the stream natural order is left to right. If your accumulator function + * is associative, consider using {@link #reduce(IntBinaryOperator)} method. + * + * @param accumulator a <a href="package-summary.html#NonInterference">non-interfering</a>, + * <a href="package-summary.html#Statelessness">stateless</a> + * function for incorporating an additional element into a result + * @return the result of the folding + * @see #foldLeft(int, IntBinaryOperator) + * @see #reduce(int, IntBinaryOperator) + * @since 10 + */ + default OptionalInt foldRight(IntBinaryOperator accumulator) { + int[] array = toArray(); + int idx = array.length; + if (idx == 0) return OptionalInt.empty(); + int result = array[--idx]; + while(idx > 0) { + int t = array[--idx]; + result = accumulator.applyAsInt(t, result); + } + return OptionalInt.of(result); + } + + /** * Performs a <a href="package-summary.html#MutableReduction">mutable * reduction</a> operation on the elements of this stream. A mutable * reduction is one in which the reduced value is a mutable result container, * such as an {@code ArrayList}, and elements are incorporated by updating * the state of the result rather than by replacing the result. This
< prev index next >