516 * handlers for both the returned and this stream are invoked.
517 *
518 * @apiNote
519 * While {@code takeWhile()} is generally a cheap operation on sequential
520 * stream pipelines, it can be quite expensive on ordered parallel
521 * pipelines, since the operation is constrained to return not just any
522 * valid prefix, but the longest prefix of elements in the encounter order.
523 * Using an unordered stream source (such as {@link #generate(Supplier)}) or
524 * removing the ordering constraint with {@link #unordered()} may result in
525 * significant speedups of {@code takeWhile()} in parallel pipelines, if the
526 * semantics of your situation permit. If consistency with encounter order
527 * is required, and you are experiencing poor performance or memory
528 * utilization with {@code takeWhile()} in parallel pipelines, switching to
529 * sequential execution with {@link #sequential()} may improve performance.
530 *
531 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
532 * <a href="package-summary.html#Statelessness">stateless</a>
533 * predicate to apply to elements to determine the longest
534 * prefix of elements.
535 * @return the new stream
536 * @since 1.9
537 */
538 default Stream<T> takeWhile(Predicate<? super T> predicate) {
539 Objects.requireNonNull(predicate);
540 // Reuses the unordered spliterator, which, when encounter is present,
541 // is safe to use as long as it configured not to split
542 return StreamSupport.stream(
543 new WhileOps.UnorderedWhileSpliterator.OfRef.Taking<>(spliterator(), true, predicate),
544 isParallel()).onClose(this::close);
545 }
546
547 /**
548 * Returns, if this stream is ordered, a stream consisting of the remaining
549 * elements of this stream after dropping the longest prefix of elements
550 * that match the given predicate. Otherwise returns, if this stream is
551 * unordered, a stream consisting of the remaining elements of this stream
552 * after dropping a subset of elements that match the given predicate.
553 *
554 * <p>If this stream is ordered then the longest prefix is a contiguous
555 * sequence of elements of this stream that match the given predicate. The
556 * first element of the sequence is the first element of this stream, and
582 * handlers for both the returned and this stream are invoked.
583 *
584 * @apiNote
585 * While {@code dropWhile()} is generally a cheap operation on sequential
586 * stream pipelines, it can be quite expensive on ordered parallel
587 * pipelines, since the operation is constrained to return not just any
588 * valid prefix, but the longest prefix of elements in the encounter order.
589 * Using an unordered stream source (such as {@link #generate(Supplier)}) or
590 * removing the ordering constraint with {@link #unordered()} may result in
591 * significant speedups of {@code dropWhile()} in parallel pipelines, if the
592 * semantics of your situation permit. If consistency with encounter order
593 * is required, and you are experiencing poor performance or memory
594 * utilization with {@code dropWhile()} in parallel pipelines, switching to
595 * sequential execution with {@link #sequential()} may improve performance.
596 *
597 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
598 * <a href="package-summary.html#Statelessness">stateless</a>
599 * predicate to apply to elements to determine the longest
600 * prefix of elements.
601 * @return the new stream
602 * @since 1.9
603 */
604 default Stream<T> dropWhile(Predicate<? super T> predicate) {
605 Objects.requireNonNull(predicate);
606 // Reuses the unordered spliterator, which, when encounter is present,
607 // is safe to use as long as it configured not to split
608 return StreamSupport.stream(
609 new WhileOps.UnorderedWhileSpliterator.OfRef.Dropping<>(spliterator(), true, predicate),
610 isParallel()).onClose(this::close);
611 }
612
613 /**
614 * Performs an action for each element of this stream.
615 *
616 * <p>This is a <a href="package-summary.html#StreamOps">terminal
617 * operation</a>.
618 *
619 * <p>The behavior of this operation is explicitly nondeterministic.
620 * For parallel stream pipelines, this operation does <em>not</em>
621 * guarantee to respect the encounter order of the stream, as doing so
622 * would sacrifice the benefit of parallelism. For any given element, the
1129
1130 /**
1131 * Returns a sequential {@code Stream} containing a single element.
1132 *
1133 * @param t the single element
1134 * @param <T> the type of stream elements
1135 * @return a singleton sequential stream
1136 */
1137 public static<T> Stream<T> of(T t) {
1138 return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
1139 }
1140
1141 /**
1142 * Returns a sequential {@code Stream} containing a single element, if
1143 * non-null, otherwise returns an empty {@code Stream}.
1144 *
1145 * @param t the single element
1146 * @param <T> the type of stream elements
1147 * @return a stream with a single element if the specified element
1148 * is non-null, otherwise an empty stream
1149 * @since 1.9
1150 */
1151 public static<T> Stream<T> ofNullable(T t) {
1152 return t == null ? Stream.empty()
1153 : StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
1154 }
1155
1156 /**
1157 * Returns a sequential ordered stream whose elements are the specified values.
1158 *
1159 * @param <T> the type of stream elements
1160 * @param values the elements of the new stream
1161 * @return the new stream
1162 */
1163 @SafeVarargs
1164 @SuppressWarnings("varargs") // Creating a stream from an array is safe
1165 public static<T> Stream<T> of(T... values) {
1166 return Arrays.stream(values);
1167 }
1168
1169 /**
|
516 * handlers for both the returned and this stream are invoked.
517 *
518 * @apiNote
519 * While {@code takeWhile()} is generally a cheap operation on sequential
520 * stream pipelines, it can be quite expensive on ordered parallel
521 * pipelines, since the operation is constrained to return not just any
522 * valid prefix, but the longest prefix of elements in the encounter order.
523 * Using an unordered stream source (such as {@link #generate(Supplier)}) or
524 * removing the ordering constraint with {@link #unordered()} may result in
525 * significant speedups of {@code takeWhile()} in parallel pipelines, if the
526 * semantics of your situation permit. If consistency with encounter order
527 * is required, and you are experiencing poor performance or memory
528 * utilization with {@code takeWhile()} in parallel pipelines, switching to
529 * sequential execution with {@link #sequential()} may improve performance.
530 *
531 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
532 * <a href="package-summary.html#Statelessness">stateless</a>
533 * predicate to apply to elements to determine the longest
534 * prefix of elements.
535 * @return the new stream
536 * @since 9
537 */
538 default Stream<T> takeWhile(Predicate<? super T> predicate) {
539 Objects.requireNonNull(predicate);
540 // Reuses the unordered spliterator, which, when encounter is present,
541 // is safe to use as long as it configured not to split
542 return StreamSupport.stream(
543 new WhileOps.UnorderedWhileSpliterator.OfRef.Taking<>(spliterator(), true, predicate),
544 isParallel()).onClose(this::close);
545 }
546
547 /**
548 * Returns, if this stream is ordered, a stream consisting of the remaining
549 * elements of this stream after dropping the longest prefix of elements
550 * that match the given predicate. Otherwise returns, if this stream is
551 * unordered, a stream consisting of the remaining elements of this stream
552 * after dropping a subset of elements that match the given predicate.
553 *
554 * <p>If this stream is ordered then the longest prefix is a contiguous
555 * sequence of elements of this stream that match the given predicate. The
556 * first element of the sequence is the first element of this stream, and
582 * handlers for both the returned and this stream are invoked.
583 *
584 * @apiNote
585 * While {@code dropWhile()} is generally a cheap operation on sequential
586 * stream pipelines, it can be quite expensive on ordered parallel
587 * pipelines, since the operation is constrained to return not just any
588 * valid prefix, but the longest prefix of elements in the encounter order.
589 * Using an unordered stream source (such as {@link #generate(Supplier)}) or
590 * removing the ordering constraint with {@link #unordered()} may result in
591 * significant speedups of {@code dropWhile()} in parallel pipelines, if the
592 * semantics of your situation permit. If consistency with encounter order
593 * is required, and you are experiencing poor performance or memory
594 * utilization with {@code dropWhile()} in parallel pipelines, switching to
595 * sequential execution with {@link #sequential()} may improve performance.
596 *
597 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
598 * <a href="package-summary.html#Statelessness">stateless</a>
599 * predicate to apply to elements to determine the longest
600 * prefix of elements.
601 * @return the new stream
602 * @since 9
603 */
604 default Stream<T> dropWhile(Predicate<? super T> predicate) {
605 Objects.requireNonNull(predicate);
606 // Reuses the unordered spliterator, which, when encounter is present,
607 // is safe to use as long as it configured not to split
608 return StreamSupport.stream(
609 new WhileOps.UnorderedWhileSpliterator.OfRef.Dropping<>(spliterator(), true, predicate),
610 isParallel()).onClose(this::close);
611 }
612
613 /**
614 * Performs an action for each element of this stream.
615 *
616 * <p>This is a <a href="package-summary.html#StreamOps">terminal
617 * operation</a>.
618 *
619 * <p>The behavior of this operation is explicitly nondeterministic.
620 * For parallel stream pipelines, this operation does <em>not</em>
621 * guarantee to respect the encounter order of the stream, as doing so
622 * would sacrifice the benefit of parallelism. For any given element, the
1129
1130 /**
1131 * Returns a sequential {@code Stream} containing a single element.
1132 *
1133 * @param t the single element
1134 * @param <T> the type of stream elements
1135 * @return a singleton sequential stream
1136 */
1137 public static<T> Stream<T> of(T t) {
1138 return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
1139 }
1140
1141 /**
1142 * Returns a sequential {@code Stream} containing a single element, if
1143 * non-null, otherwise returns an empty {@code Stream}.
1144 *
1145 * @param t the single element
1146 * @param <T> the type of stream elements
1147 * @return a stream with a single element if the specified element
1148 * is non-null, otherwise an empty stream
1149 * @since 9
1150 */
1151 public static<T> Stream<T> ofNullable(T t) {
1152 return t == null ? Stream.empty()
1153 : StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false);
1154 }
1155
1156 /**
1157 * Returns a sequential ordered stream whose elements are the specified values.
1158 *
1159 * @param <T> the type of stream elements
1160 * @param values the elements of the new stream
1161 * @return the new stream
1162 */
1163 @SafeVarargs
1164 @SuppressWarnings("varargs") // Creating a stream from an array is safe
1165 public static<T> Stream<T> of(T... values) {
1166 return Arrays.stream(values);
1167 }
1168
1169 /**
|