src/share/classes/java/util/stream/Stream.java

Print this page
rev 7627 : 8015315: Stream.concat methods
Reviewed-by: psandoz
Contributed-by: brian.goetz@oracle.com, henry.jen@oracle.com
rev 7630 : 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
Reviewed-by: henryjen
rev 7631 : 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
Reviewed-by: psandoz
Contributed-by: brian goetz <brian.goetz@oracle.com>
rev 7632 : 8015318: Extend Collector with 'finish' operation
Reviewed-by:
Contributed-by: brian.goetz@oracle.com
rev 7633 : 8017513: Support for closeable streams
Reviewed-by:
Contributed-by: brian.goetz@oracle.com


 873     /**
 874      * Returns a sequential {@code Stream} where each element is
 875      * generated by a {@code Supplier}.  This is suitable for generating
 876      * constant streams, streams of random elements, etc.
 877      *
 878      * @param <T> the type of stream elements
 879      * @param s the {@code Supplier} of generated elements
 880      * @return a new sequential {@code Stream}
 881      */
 882     public static<T> Stream<T> generate(Supplier<T> s) {
 883         Objects.requireNonNull(s);
 884         return StreamSupport.stream(
 885                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
 886     }
 887 
 888     /**
 889      * Creates a lazy concatenated {@code Stream} whose elements are all the
 890      * elements of a first {@code Stream} succeeded by all the elements of the
 891      * second {@code Stream}. The resulting stream is ordered if both
 892      * of the input streams are ordered, and parallel if either of the input
 893      * streams is parallel.

 894      *
 895      * @param <T> The type of stream elements
 896      * @param a the first stream
 897      * @param b the second stream to concatenate on to end of the first
 898      *        stream
 899      * @return the concatenation of the two input streams
 900      */
 901     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
 902         Objects.requireNonNull(a);
 903         Objects.requireNonNull(b);
 904 
 905         @SuppressWarnings("unchecked")
 906         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
 907                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
 908         return StreamSupport.stream(split, a.isParallel() || b.isParallel());

 909     }
 910 
 911     /**
 912      * A mutable builder for a {@code Stream}.  This allows the creation of a
 913      * {@code Stream} by generating elements individually and adding them to the
 914      * {@code Builder} (without the copying overhead that comes from using
 915      * an {@code ArrayList} as a temporary buffer.)
 916      *
 917      * <p>A {@code Stream.Builder} has a lifecycle, where it starts in a building
 918      * phase, during which elements can be added, and then transitions to a built
 919      * phase, after which elements may not be added.  The built phase begins
 920      * when the {@link #build()} method is called, which creates an ordered
 921      * {@code Stream} whose elements are the elements that were added to the stream
 922      * builder, in the order they were added.
 923      *
 924      * @param <T> the type of stream elements
 925      * @see Stream#builder()
 926      * @since 1.8
 927      */
 928     public interface Builder<T> extends Consumer<T> {




 873     /**
 874      * Returns a sequential {@code Stream} where each element is
 875      * generated by a {@code Supplier}.  This is suitable for generating
 876      * constant streams, streams of random elements, etc.
 877      *
 878      * @param <T> the type of stream elements
 879      * @param s the {@code Supplier} of generated elements
 880      * @return a new sequential {@code Stream}
 881      */
 882     public static<T> Stream<T> generate(Supplier<T> s) {
 883         Objects.requireNonNull(s);
 884         return StreamSupport.stream(
 885                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
 886     }
 887 
 888     /**
 889      * Creates a lazy concatenated {@code Stream} whose elements are all the
 890      * elements of a first {@code Stream} succeeded by all the elements of the
 891      * second {@code Stream}. The resulting stream is ordered if both
 892      * of the input streams are ordered, and parallel if either of the input
 893      * streams is parallel.  When the resulting stream is closed, the close
 894      * handlers for both input streams is invoked.
 895      *
 896      * @param <T> The type of stream elements
 897      * @param a the first stream
 898      * @param b the second stream to concatenate on to end of the first
 899      *        stream
 900      * @return the concatenation of the two input streams
 901      */
 902     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
 903         Objects.requireNonNull(a);
 904         Objects.requireNonNull(b);
 905 
 906         @SuppressWarnings("unchecked")
 907         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
 908                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
 909         Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
 910         return stream.onClose(Streams.composedClose(a, b));
 911     }
 912 
 913     /**
 914      * A mutable builder for a {@code Stream}.  This allows the creation of a
 915      * {@code Stream} by generating elements individually and adding them to the
 916      * {@code Builder} (without the copying overhead that comes from using
 917      * an {@code ArrayList} as a temporary buffer.)
 918      *
 919      * <p>A {@code Stream.Builder} has a lifecycle, where it starts in a building
 920      * phase, during which elements can be added, and then transitions to a built
 921      * phase, after which elements may not be added.  The built phase begins
 922      * when the {@link #build()} method is called, which creates an ordered
 923      * {@code Stream} whose elements are the elements that were added to the stream
 924      * builder, in the order they were added.
 925      *
 926      * @param <T> the type of stream elements
 927      * @see Stream#builder()
 928      * @since 1.8
 929      */
 930     public interface Builder<T> extends Consumer<T> {