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

Print this page
rev 7532 : 8015315: Stream.concat methods
Contributed-by: brian.goetz@oracle.com, henry.jen@oracle.com


 866         };
 867         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
 868                 iterator,
 869                 Spliterator.ORDERED | Spliterator.IMMUTABLE));
 870     }
 871 
 872     /**
 873      * Returns a sequential {@code Stream} where each element is
 874      * generated by a {@code Supplier}.  This is suitable for generating
 875      * constant streams, streams of random elements, etc.
 876      *
 877      * @param <T> the type of stream elements
 878      * @param s the {@code Supplier} of generated elements
 879      * @return a new sequential {@code Stream}
 880      */
 881     public static<T> Stream<T> generate(Supplier<T> s) {
 882         Objects.requireNonNull(s);
 883         return StreamSupport.stream(
 884                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s));
 885     }

























 886 }


 866         };
 867         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
 868                 iterator,
 869                 Spliterator.ORDERED | Spliterator.IMMUTABLE));
 870     }
 871 
 872     /**
 873      * Returns a sequential {@code Stream} where each element is
 874      * generated by a {@code Supplier}.  This is suitable for generating
 875      * constant streams, streams of random elements, etc.
 876      *
 877      * @param <T> the type of stream elements
 878      * @param s the {@code Supplier} of generated elements
 879      * @return a new sequential {@code Stream}
 880      */
 881     public static<T> Stream<T> generate(Supplier<T> s) {
 882         Objects.requireNonNull(s);
 883         return StreamSupport.stream(
 884                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s));
 885     }
 886 
 887     /**
 888      * Creates a lazy concatenated {@code Stream} whose elements are all the
 889      * elements of a first {@code Stream} succeeded by all the elements of the
 890      * second {@code Stream}. The resulting stream is ordered if both
 891      * of the input streams are ordered, and parallel if either of the input
 892      * streams is parallel.
 893      *
 894      * @param <T> The type of stream elements
 895      * @param a the first stream
 896      * @param b the second stream to concatenate on to end of the first
 897      *        stream
 898      * @return the concatenation of the two input streams
 899      */
 900     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
 901         Objects.requireNonNull(a);
 902         Objects.requireNonNull(b);
 903 
 904         @SuppressWarnings("unchecked")
 905         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
 906                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
 907         return (a.isParallel() || b.isParallel())
 908                ? StreamSupport.parallelStream(split)
 909                : StreamSupport.stream(split);
 910     }
 911 }