562 * can be parallelized without requiring additional synchronization. |
562 * can be parallelized without requiring additional synchronization. |
563 * |
563 * |
564 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
564 * <p>This is a <a href="package-summary.html#StreamOps">terminal |
565 * operation</a>. |
565 * operation</a>. |
566 * |
566 * |
567 * @param <R> type of the result |
567 * @param <R> the type of the mutable result container |
568 * @param supplier a function that creates a new result container. For a |
568 * @param supplier a function that creates a new mutable result container. |
569 * parallel execution, this function may be called |
569 * For a parallel execution, this function may be called |
570 * multiple times and must return a fresh value each time. |
570 * multiple times and must return a fresh value each time. |
571 * @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
571 * @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
572 * <a href="package-summary.html#NonInterference">non-interfering</a>, |
572 * <a href="package-summary.html#NonInterference">non-interfering</a>, |
573 * <a href="package-summary.html#Statelessness">stateless</a> |
573 * <a href="package-summary.html#Statelessness">stateless</a> |
574 * function for incorporating an additional element into a result |
574 * function that must fold an element into a result |
|
575 * container. |
575 * @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
576 * @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
576 * <a href="package-summary.html#NonInterference">non-interfering</a>, |
577 * <a href="package-summary.html#NonInterference">non-interfering</a>, |
577 * <a href="package-summary.html#Statelessness">stateless</a> |
578 * <a href="package-summary.html#Statelessness">stateless</a> |
578 * function for combining two values, which must be |
579 * function that accepts two partial result containers |
579 * compatible with the accumulator function |
580 * and merges them, which must be compatible with the |
|
581 * accumulator function. The combiner function must fold |
|
582 * the elements from the second result container into the |
|
583 * first result container. |
580 * @return the result of the reduction |
584 * @return the result of the reduction |
581 * @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
585 * @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
582 */ |
586 */ |
583 <R> R collect(Supplier<R> supplier, |
587 <R> R collect(Supplier<R> supplier, |
584 ObjLongConsumer<R> accumulator, |
588 ObjLongConsumer<R> accumulator, |
874 * |
878 * |
875 * <p>The first element (position {@code 0}) in the {@code LongStream} will |
879 * <p>The first element (position {@code 0}) in the {@code LongStream} will |
876 * be the provided {@code seed}. For {@code n > 0}, the element at position |
880 * be the provided {@code seed}. For {@code n > 0}, the element at position |
877 * {@code n}, will be the result of applying the function {@code f} to the |
881 * {@code n}, will be the result of applying the function {@code f} to the |
878 * element at position {@code n - 1}. |
882 * element at position {@code n - 1}. |
|
883 * |
|
884 * <p>The action of applying {@code f} for one element |
|
885 * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> |
|
886 * the action of applying {@code f} for subsequent elements. For any given |
|
887 * element the action may be performed in whatever thread the library |
|
888 * chooses. |
879 * |
889 * |
880 * @param seed the initial element |
890 * @param seed the initial element |
881 * @param f a function to be applied to the previous element to produce |
891 * @param f a function to be applied to the previous element to produce |
882 * a new element |
892 * a new element |
883 * @return a new sequential {@code LongStream} |
893 * @return a new sequential {@code LongStream} |
906 return StreamSupport.longStream(spliterator, false); |
916 return StreamSupport.longStream(spliterator, false); |
907 } |
917 } |
908 |
918 |
909 /** |
919 /** |
910 * Returns a sequential ordered {@code LongStream} produced by iterative |
920 * Returns a sequential ordered {@code LongStream} produced by iterative |
911 * application of a function to an initial element, conditioned on |
921 * application of the given {@code next} function to an initial element, |
912 * satisfying the supplied predicate. The stream terminates as soon as |
922 * conditioned on satisfying the given {@code hasNext} predicate. The |
913 * the predicate returns false. |
923 * stream terminates as soon as the {@code hasNext} predicate returns false. |
914 * |
924 * |
915 * <p> |
925 * <p>{@code LongStream.iterate} should produce the same sequence of elements as |
916 * {@code LongStream.iterate} should produce the same sequence of elements |
926 * produced by the corresponding for-loop: |
917 * as produced by the corresponding for-loop: |
927 * <pre>{@code |
918 * <pre>{@code |
928 * for (long index=seed; hasNext.test(index); index = next.applyAsLong(index)) { |
919 * for (long index=seed; predicate.test(index); index = f.applyAsLong(index)) { |
|
920 * ... |
929 * ... |
921 * } |
930 * } |
922 * }</pre> |
931 * }</pre> |
923 * |
932 * |
924 * <p> |
933 * <p>The resulting sequence may be empty if the {@code hasNext} predicate |
925 * The resulting sequence may be empty if the predicate does not hold on |
934 * does not hold on the seed value. Otherwise the first element will be the |
926 * the seed value. Otherwise the first element will be the supplied seed |
935 * supplied {@code seed} value, the next element (if present) will be the |
927 * value, the next element (if present) will be the result of applying the |
936 * result of applying the {@code next} function to the {@code seed} value, |
928 * function f to the seed value, and so on iteratively until the predicate |
937 * and so on iteratively until the {@code hasNext} predicate indicates that |
929 * indicates that the stream should terminate. |
938 * the stream should terminate. |
|
939 * |
|
940 * <p>The action of applying the {@code hasNext} predicate to an element |
|
941 * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> |
|
942 * the action of applying the {@code next} function to that element. The |
|
943 * action of applying the {@code next} function for one element |
|
944 * <i>happens-before</i> the action of applying the {@code hasNext} |
|
945 * predicate for subsequent elements. For any given element an action may |
|
946 * be performed in whatever thread the library chooses. |
930 * |
947 * |
931 * @param seed the initial element |
948 * @param seed the initial element |
932 * @param predicate a predicate to apply to elements to determine when the |
949 * @param hasNext a predicate to apply to elements to determine when the |
933 * stream must terminate. |
950 * stream must terminate. |
934 * @param f a function to be applied to the previous element to produce |
951 * @param next a function to be applied to the previous element to produce |
935 * a new element |
952 * a new element |
936 * @return a new sequential {@code LongStream} |
953 * @return a new sequential {@code LongStream} |
937 * @since 9 |
954 * @since 9 |
938 */ |
955 */ |
939 public static LongStream iterate(long seed, LongPredicate predicate, LongUnaryOperator f) { |
956 public static LongStream iterate(long seed, LongPredicate hasNext, LongUnaryOperator next) { |
940 Objects.requireNonNull(f); |
957 Objects.requireNonNull(next); |
941 Objects.requireNonNull(predicate); |
958 Objects.requireNonNull(hasNext); |
942 Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, |
959 Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, |
943 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { |
960 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { |
944 long prev; |
961 long prev; |
945 boolean started, finished; |
962 boolean started, finished; |
946 |
963 |