Class Streams

    • Method Detail

      • stream

        @Beta
        public static <T> Stream<T> stream​(Iterator<T> iterator)
        Returns a sequential Stream of the remaining contents of iterator. Do not use iterator directly after passing it to this method.
      • stream

        @Beta
        public static <T> Stream<T> stream​(Optional<T> optional)
        If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.
      • stream

        @Beta
        public static <T> Stream<T> stream​(Optional<T> optional)
        If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

        Java 9 users: use optional.stream() instead.

      • stream

        @Beta
        public static IntStream stream​(OptionalInt optional)
        If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

        Java 9 users: use optional.stream() instead.

      • stream

        @Beta
        public static LongStream stream​(OptionalLong optional)
        If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

        Java 9 users: use optional.stream() instead.

      • stream

        @Beta
        public static DoubleStream stream​(OptionalDouble optional)
        If a value is present in optional, returns a stream containing only that element, otherwise returns an empty stream.

        Java 9 users: use optional.stream() instead.

      • concat

        @SafeVarargs
        public static <T> Stream<T> concat​(Stream<? extends T>... streams)
        Returns a Stream containing the elements of the first stream, followed by the elements of the second stream, and so on.

        This is equivalent to Stream.of(streams).flatMap(stream -> stream), but the returned stream may perform better.

        See Also:
        Stream.concat(Stream, Stream)
      • concat

        public static IntStream concat​(IntStream... streams)
        Returns an IntStream containing the elements of the first stream, followed by the elements of the second stream, and so on.

        This is equivalent to Stream.of(streams).flatMapToInt(stream -> stream), but the returned stream may perform better.

        See Also:
        IntStream.concat(IntStream, IntStream)
      • forEachPair

        @Beta
        public static <A,​B> void forEachPair​(Stream<A> streamA,
                                                   Stream<B> streamB,
                                                   BiConsumer<? super A,​? super B> consumer)
        Invokes consumer once for each pair of corresponding elements in streamA and streamB. If one stream is longer than the other, the extra elements are silently ignored. Elements passed to the consumer are guaranteed to come from the same position in their respective source streams. For example:
        
         Streams.forEachPair(
           Stream.of("foo1", "foo2", "foo3"),
           Stream.of("bar1", "bar2"),
           (arg1, arg2) -> System.out.println(arg1 + ":" + arg2)
         

        will print:

        
         foo1:bar1
         foo2:bar2
         

        Warning: If either supplied stream is a parallel stream, the same correspondence between elements will be made, but the order in which those pairs of elements are passed to the consumer is not defined.

        Note that many usages of this method can be replaced with simpler calls to zip(java.util.stream.Stream<A>, java.util.stream.Stream<B>, java.util.function.BiFunction<? super A, ? super B, R>). This method behaves equivalently to zipping the stream elements into temporary pair objects and then using Stream.forEach(java.util.function.Consumer<? super T>) on that stream.

        Since:
        22.0
      • mapWithIndex

        @Beta
        public static <T,​R> Stream<R> mapWithIndex​(Stream<T> stream,
                                                         Streams.FunctionWithIndex<? super T,​? extends R> function)
        Returns a stream consisting of the results of applying the given function to the elements of stream and their indices in the stream. For example,
        
         mapWithIndex(
             Stream.of("a", "b", "c"),
             (str, index) -> str + ":" + index)
         

        would return Stream.of("a:0", "b:1", "c:2").

        The resulting stream is efficiently splittable if and only if stream was efficiently splittable and its underlying spliterator reported Spliterator.SUBSIZED. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.

        The order of the resulting stream is defined if and only if the order of the original stream was defined.

      • mapWithIndex

        @Beta
        public static <R> Stream<R> mapWithIndex​(IntStream stream,
                                                 Streams.IntFunctionWithIndex<R> function)
        Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream. For example,
        
         mapWithIndex(
             IntStream.of(0, 1, 2),
             (i, index) -> i + ":" + index)
         

        ...would return Stream.of("0:0", "1:1", "2:2").

        The resulting stream is efficiently splittable if and only if stream was efficiently splittable and its underlying spliterator reported Spliterator.SUBSIZED. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.

        The order of the resulting stream is defined if and only if the order of the original stream was defined.

      • mapWithIndex

        @Beta
        public static <R> Stream<R> mapWithIndex​(LongStream stream,
                                                 Streams.LongFunctionWithIndex<R> function)
        Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream. For example,
        
         mapWithIndex(
             LongStream.of(0, 1, 2),
             (i, index) -> i + ":" + index)
         

        ...would return Stream.of("0:0", "1:1", "2:2").

        The resulting stream is efficiently splittable if and only if stream was efficiently splittable and its underlying spliterator reported Spliterator.SUBSIZED. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.

        The order of the resulting stream is defined if and only if the order of the original stream was defined.

      • mapWithIndex

        @Beta
        public static <R> Stream<R> mapWithIndex​(DoubleStream stream,
                                                 Streams.DoubleFunctionWithIndex<R> function)
        Returns a stream consisting of the results of applying the given function to the elements of stream and their indexes in the stream. For example,
        
         mapWithIndex(
             DoubleStream.of(0, 1, 2),
             (x, index) -> x + ":" + index)
         

        ...would return Stream.of("0.0:0", "1.0:1", "2.0:2").

        The resulting stream is efficiently splittable if and only if stream was efficiently splittable and its underlying spliterator reported Spliterator.SUBSIZED. This is generally the case if the underlying stream comes from a data structure supporting efficient indexed random access, typically an array or list.

        The order of the resulting stream is defined if and only if the order of the original stream was defined.