Class Streams


  • public final class Streams
    extends Object
    Convenience methods for creating and manipulating Java streams
    Author:
    David B. Bracewell
    • Method Detail

      • reusableStream

        public static <T> Stream<T> reusableStream​(@NonNull
                                                   @NonNull Collection<T> collection)
      • reusableParallelStream

        public static <T> Stream<T> reusableParallelStream​(@NonNull
                                                           @NonNull Collection<T> collection)
      • reusableStream

        @SafeVarargs
        public static <T> Stream<T> reusableStream​(@NonNull
                                                   @NonNull T... objects)
      • reusableIntStream

        public static IntStream reusableIntStream​(int... objects)
      • reusableLongStream

        public static LongStream reusableLongStream​(long... objects)
      • reusableDoubleStream

        public static DoubleStream reusableDoubleStream​(@NonNull
                                                        @lombok.NonNull double... objects)
      • asStream

        public static Stream<String> asStream​(InputStream stream)
        Creates a Stream of String lines from an InputStream using a CharsetDetectingReader to read from the input stream.
        Parameters:
        stream - the stream to read from
        Returns:
        the stream of strings from the input stream
      • asStream

        public static Stream<String> asStream​(Reader reader)
        Creates a Stream of String lines from an Reader by converting the reader into a BufferedReader and calling the lines method
        Parameters:
        reader - the reader to read from
        Returns:
        the stream of strings from the input stream
      • asParallelStream

        public static <T> Stream<T> asParallelStream​(Iterator<? extends T> iterator)
        Creates a parallel stream from a given iterator
        Type Parameters:
        T - the element type of the iterator parameter
        Parameters:
        iterator - the iterator
        Returns:
        the stream
      • asParallelStream

        public static <T> Stream<T> asParallelStream​(Iterable<? extends T> iterable)
        Creates a parallel stream for a given iterable
        Type Parameters:
        T - the element type of the iterable parameter
        Parameters:
        iterable - the iterable
        Returns:
        the stream
      • asStream

        @SafeVarargs
        public static <T> Stream<T> asStream​(T... values)
        Creates a Stream from a number of arguments returning an empty string if nothing is passed
        Type Parameters:
        T - the value type parameter
        Parameters:
        values - the values
        Returns:
        the stream
      • asStream

        public static <T> Stream<T> asStream​(Iterator<? extends T> iterator)
        Creates a stream from a given iterator
        Type Parameters:
        T - the element type of the iterator parameter
        Parameters:
        iterator - the iterator
        Returns:
        the stream
      • asStream

        public static <T> Stream<T> asStream​(Iterator<? extends T> iterator,
                                             boolean parallel)
        Creates a parallel stream from a given iterator
        Type Parameters:
        T - the element type of the iterator parameter
        Parameters:
        iterator - the iterator
        parallel - True - create a parallel stream, False create a sequential stream
        Returns:
        the stream
      • asStream

        public static <T> Stream<T> asStream​(Iterable<? extends T> iterable)
        Convenience method for creating a stream from an iterable
        Type Parameters:
        T - the iterable element type parameter
        Parameters:
        iterable - the iterable
        Returns:
        the stream
      • asStream

        public static <T> Stream<T> asStream​(Iterable<? extends T> iterable,
                                             boolean parallel)
        Convenience method for creating a stream from an iterable
        Type Parameters:
        T - the iterable element type parameter
        Parameters:
        iterable - the iterable
        parallel - True create a parallel stream, False create sequential stream
        Returns:
        the stream
      • zip

        public static <T,​U> Stream<Map.Entry<T,​U>> zip​(Stream<? extends T> stream1,
                                                                   Stream<? extends U> stream2)

        Zips (combines) items from both streams as tuples. Length of the new stream will the minimum length of the two streams.

        Type Parameters:
        T - the first stream's element type parameter
        U - the second stream's element type parameter
        Parameters:
        stream1 - the first stream * @param stream2 the second stream
        Returns:
        the zipped stream
      • zipWithIndex

        public static <T> Stream<Map.Entry<T,​Long>> zipWithIndex​(Stream<T> stream)

        Zips (combines) items from a stream with an integer in order of access.

        Type Parameters:
        T - the stream's type parameter
        Parameters:
        stream - the stream
        Returns:
        the stream
      • flatten

        public static <T> Stream<T> flatten​(Iterable<? extends Iterable<? extends T>> iterables)
        Flattens an iterable of iterable into a single stream
        Type Parameters:
        T - the type parameter
        Parameters:
        iterables - the iterables
        Returns:
        the stream
      • union

        public static <T> Stream<T> union​(Collection<? extends T> c1,
                                          Collection<? extends T> c2)
        Concatenates two collections into a single stream
        Type Parameters:
        T - the type parameter
        Parameters:
        c1 - the first collection
        c2 - the second collection
        Returns:
        the stream
      • intersection

        public static <T> Stream<T> intersection​(Collection<? extends T> c1,
                                                 Collection<? extends T> c2)
        Creates a new stream that is the intersection of the two collections
        Type Parameters:
        T - the type parameter
        Parameters:
        c1 - the first collection
        c2 - the second collection
        Returns:
        the stream
      • difference

        public static <T> Stream<T> difference​(Collection<? extends T> c1,
                                               Collection<? extends T> c2)
        Creates a new stream that has all elements of the first collection that are not in the second collection
        Type Parameters:
        T - the type parameter
        Parameters:
        c1 - the first collection
        c2 - the second collection
        Returns:
        the stream
      • partition

        public static <T> Stream<Stream<T>> partition​(Stream<T> stream,
                                                      long partitionSize)