Package com.gengoai.stream
Class Streams
- java.lang.Object
-
- com.gengoai.stream.Streams
-
public final class Streams extends Object
Convenience methods for creating and manipulating Java streams- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Stream<T>
asParallelStream(Iterable<? extends T> iterable)
Creates a parallel stream for a given iterablestatic <T> Stream<T>
asParallelStream(Iterator<? extends T> iterator)
Creates a parallel stream from a given iteratorstatic Stream<String>
asStream(InputStream stream)
Creates aStream
ofString
lines from anInputStream
using aCharsetDetectingReader
to read from the input stream.static Stream<String>
asStream(Reader reader)
Creates aStream
ofString
lines from anReader
by converting the reader into aBufferedReader
and calling the lines methodstatic <T> Stream<T>
asStream(Iterable<? extends T> iterable)
Convenience method for creating a stream from an iterablestatic <T> Stream<T>
asStream(Iterable<? extends T> iterable, boolean parallel)
Convenience method for creating a stream from an iterablestatic <T> Stream<T>
asStream(Iterator<? extends T> iterator)
Creates a stream from a given iteratorstatic <T> Stream<T>
asStream(Iterator<? extends T> iterator, boolean parallel)
Creates a parallel stream from a given iteratorstatic <T> Stream<T>
asStream(T... values)
Creates aStream
from a number of arguments returning an empty string if nothing is passedstatic <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 collectionstatic <T> Stream<T>
flatten(Iterable<? extends Iterable<? extends T>> iterables)
Flattens an iterable of iterable into a single streamstatic <T> Stream<T>
intersection(Collection<? extends T> c1, Collection<? extends T> c2)
Creates a new stream that is the intersection of the two collectionsstatic <T> Stream<Stream<T>>
partition(Stream<T> stream, long partitionSize)
static DoubleStream
reusableDoubleStream(@lombok.NonNull double... objects)
static DoubleStream
reusableDoubleStream(@NonNull SerializableSupplier<DoubleStream> streamSupplier)
static IntStream
reusableIntStream(int... objects)
static IntStream
reusableIntStream(@NonNull SerializableSupplier<IntStream> streamSupplier)
static LongStream
reusableLongStream(long... objects)
static LongStream
reusableLongStream(@NonNull SerializableSupplier<LongStream> streamSupplier)
static <T> Stream<T>
reusableParallelStream(@NonNull Collection<T> collection)
static <T> Stream<T>
reusableStream(@NonNull SerializableSupplier<Stream<T>> streamSupplier)
static <T> Stream<T>
reusableStream(@NonNull Collection<T> collection)
static <T> Stream<T>
reusableStream(@NonNull T... objects)
static <T> Stream<T>
union(Collection<? extends T> c1, Collection<? extends T> c2)
Concatenates two collections into a single streamstatic <T,U>
Stream<Map.Entry<T,U>>zip(Stream<? extends T> stream1, Stream<? extends U> stream2)
Zips (combines) items from both streams as tuples.static <T> Stream<Map.Entry<T,Long>>
zipWithIndex(Stream<T> stream)
Zips (combines) items from a stream with an integer in order of access.
-
-
-
Method Detail
-
reusableStream
public static <T> Stream<T> reusableStream(@NonNull @NonNull Collection<T> collection)
-
reusableStream
public static <T> Stream<T> reusableStream(@NonNull @NonNull SerializableSupplier<Stream<T>> streamSupplier)
-
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)
-
reusableIntStream
public static IntStream reusableIntStream(@NonNull @NonNull SerializableSupplier<IntStream> streamSupplier)
-
reusableLongStream
public static LongStream reusableLongStream(long... objects)
-
reusableLongStream
public static LongStream reusableLongStream(@NonNull @NonNull SerializableSupplier<LongStream> streamSupplier)
-
reusableDoubleStream
public static DoubleStream reusableDoubleStream(@NonNull @lombok.NonNull double... objects)
-
reusableDoubleStream
public static DoubleStream reusableDoubleStream(@NonNull @NonNull SerializableSupplier<DoubleStream> streamSupplier)
-
asStream
public static Stream<String> asStream(InputStream stream)
Creates aStream
ofString
lines from anInputStream
using aCharsetDetectingReader
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 aStream
ofString
lines from anReader
by converting the reader into aBufferedReader
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 aStream
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 iteratorparallel
- 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 iterableparallel
- 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 parameterU
- 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 collectionc2
- 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 collectionc2
- 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 collectionc2
- the second collection- Returns:
- the stream
-
-