< prev index next >

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

Print this page

        

@@ -543,10 +543,153 @@
      * @see #reduce(long, LongBinaryOperator)
      */
     OptionalLong reduce(LongBinaryOperator 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
+     *     long result = seed;
+     *     for (long 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(long, LongBinaryOperator)} 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(long, LongBinaryOperator)
+     * @see #foldLeft(LongBinaryOperator)
+     * @since 10
+     */
+    default long foldLeft(long seed, LongBinaryOperator accumulator) {
+        ReduceOps.LongFoldLeftOp op = new ReduceOps.LongFoldLeftOp(accumulator);
+        op.accept(seed);
+        forEachOrdered(op);
+        return op.getAsLong();
+    }
+
+    /**
+     * Folds the elements of this stream using the provided accumulation
+     * function, going left to right. This is equivalent to:
+     *
+     * <pre>
+     * {@code
+     *     boolean foundAny = false;
+     *     long result = 0;
+     *     for (long element : this stream) {
+     *         if (!foundAny) {
+     *             foundAny = true;
+     *             result = element;
+     *         }
+     *         else
+     *             result = accumulator.apply(result, element);
+     *     }
+     *     return foundAny ? OptionalLong.of(result) : OptionalLong.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(LongBinaryOperator)} 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(long, LongBinaryOperator)
+     * @see #reduce(LongBinaryOperator)
+     * @since 10
+     */
+    default OptionalLong foldLeft(LongBinaryOperator accumulator) {
+        ReduceOps.LongFoldLeftOp op = new ReduceOps.LongFoldLeftOp(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(long, LongBinaryOperator)} 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(long, LongBinaryOperator)
+     * @see #reduce(long, LongBinaryOperator)
+     * @since 10
+     */
+    default long foldRight(long seed, LongBinaryOperator accumulator) {
+        long[] array = toArray();
+        long result = seed;
+        for(int idx = array.length - 1; idx >= 0; idx--) {
+            result = accumulator.applyAsLong(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(LongBinaryOperator)} 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(long, LongBinaryOperator)
+     * @see #reduce(long, LongBinaryOperator)
+     * @since 10
+     */
+    default OptionalLong foldRight(LongBinaryOperator accumulator) {
+        long[] array = toArray();
+        int idx = array.length;
+        if (idx == 0) return OptionalLong.empty();
+        long result = array[--idx];
+        while(idx > 0) {
+            long t = array[--idx];
+            result = accumulator.applyAsLong(t, result);
+        }
+        return OptionalLong.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 >