Additional top-level class in compilation unit: StreamModule.
Stream is lazy sequence of elements which may be infinitely long.
Its immutability makes it suitable for concurrent programming.
A Stream is composed of a head element and a lazy evaluated tail Stream.
There are two implementations of the Stream interface:
Empty, which represents the empty Stream.Cons, which represents a Stream containing one or more elements.Stream:
// factory methods
Stream.empty() // = Stream.of() = Nil.instance()
Stream.of(x) // = new Cons<>(x, Nil.instance())
Stream.of(Object...) // e.g. Stream.of(1, 2, 3)
Stream.ofAll(Iterable) // e.g. Stream.ofAll(List.of(1, 2, 3)) = 1, 2, 3
Stream.ofAll(<primitive array>) // e.g. List.ofAll(1, 2, 3) = 1, 2, 3
// int sequences
Stream.from(0) // = 0, 1, 2, 3, ...
Stream.range(0, 3) // = 0, 1, 2
Stream.rangeClosed(0, 3) // = 0, 1, 2, 3
// generators
Stream.cons(Object, Supplier) // e.g. Stream.cons(current, () -> next(current));
Stream.continually(Supplier) // e.g. Stream.continually(Math::random);
Stream.iterate(Object, Function)// e.g. Stream.iterate(1, i -> i * 2);
Factory method applications:
Stream<Integer> s1 = Stream.of(1);
Stream<Integer> s2 = Stream.of(1, 2, 3);
// = Stream.of(new Integer[] {1, 2, 3});
Stream<int[]> s3 = Stream.ofAll(1, 2, 3);
Stream<List<Integer>> s4 = Stream.ofAll(List.of(1, 2, 3));
Stream<Integer> s5 = Stream.ofAll(1, 2, 3);
Stream<Integer> s6 = Stream.ofAll(List.of(1, 2, 3));
// cuckoo's egg
Stream<Integer[]> s7 = Stream.<Integer[]> of(new Integer[] {1, 2, 3});
Example: Generating prime numbers
// = Stream(2L, 3L, 5L, 7L, ...)
Stream.iterate(2L, PrimeNumbers::nextPrimeFrom)
// helpers
static long nextPrimeFrom(long num) {
return Stream.from(num + 1).find(PrimeNumbers::isPrime).get();
}
static boolean isPrime(long num) {
return !Stream.rangeClosed(2L, (long) Math.sqrt(num)).exists(d -> num % d == 0);
}
See Okasaki, Chris: Purely Functional Data Structures (p. 34 ff.). Cambridge, 2003.
| Modifier and Type | Class and Description |
|---|---|
| public abstract static class | |
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| public static final long |
| Modifier and Type | Method and Description |
|---|---|
| public default Stream | append(T
An element element)Implements io. Appends an element to this. |
| public default Stream | appendAll(Iterable<? extends T>
An Iterable of elements elements)Implements io. Appends all given elements to this. |
| public default Stream | Returns: a new Streaman mapper mapper)Appends itself to the end of stream with |
| public default List | asJava()
Implements io. Creates an immutable |
| public default Stream | asJava(Consumer<? super List<T>>
A side-effecting unit of work that operates on an immutable action)java.util.List view.Implements io. Creates an immutable |
| public default List | asJavaMutable()
Implements io. Creates a mutable |
| public default Stream | asJavaMutable(Consumer<? super List<T>>
A side-effecting unit of work that operates on a mutable action)java.util.List view.Implements io. Creates a mutable |
| public default <R> Stream | collect(PartialFunction<? super T, ? extends R>
A function that is not necessarily defined of all elements of this traversable. partialFunction)Implements io. Collects all elements that are in the domain of the given |
| public static < Component type of the Stream. T> Collector | Returns: A io.vavr.collection.Stream Collector.Returns a |
| public default Stream | combinations()
Implements io. Returns the union of all combinations from k = 0 to length(). |
| public default Stream | combinations(int
Size of subsets k)Implements io. Returns the k-combination of this traversable, i.e. all subset of this of k distinct elements. |
| public static < Component type. T> Stream | |
| public static < Component type. T> Stream | |
| public static < value type T> Stream | Returns: A new StreamThe head element of the Stream head, Supplier<? extends Stream<? extends T>> A supplier of the tail values. To end the stream, return tailSupplier)Stream#empty.Constructs a Stream of a head element and a tail supplier. |
| public static < value type T> Stream | Returns: A new StreamA Supplier of Stream values supplier)Generates an (theoretically) infinitely long Stream using a value Supplier. |
| public static < Element type T> Stream | Returns: A new Stream containing infinitet's.An element t)Repeats an element infinitely often. |
| public default Iterator | crossProduct(int
the number of cartesian multiplications power)Implements io. Calculates the n-ary cartesian power (or cross product or simply product) of this. |
| public default Stream | Returns: A new Stream containing this elements cycled.Repeat the elements of this Stream infinitely. |
| public default Stream | Returns: A new Stream containing this elements cycledcount times.the number of cycles to be performed count)Repeat the elements of this Stream |
| public default Stream | distinct()
Implements io. Returns a new version of this which contains no duplicates. |
| public default Stream | distinctBy(Comparator<? super T>
A comparator comparator)Implements io. Returns a new version of this which contains no duplicates. |
| public default <U> Stream | distinctBy(Function<? super T, ? extends U>
A key extractor keyExtractor)Implements io. Returns a new version of this which contains no duplicates. |
| public default Stream | drop(int
The number of elements to drop. n)Implements io. Drops the first n elements of this or all elements, if this length < n. |
| public default Stream | dropRight(int
The number of elements to drop. n)Implements io. Drops the last n elements of this or all elements, if this length < n. |
| public default Stream | dropRightUntil(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Drops elements until the predicate holds for the current element, starting from the end. |
| public default Stream | dropRightWhile(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Drops elements while the predicate holds for the current element, starting from the end. |
| public default Stream | dropUntil(Predicate<? super T>
A condition tested subsequently for this elements. predicate)Implements io. Drops elements until the predicate holds for the current element. |
| public default Stream | dropWhile(Predicate<? super T>
A condition tested subsequently for this elements. predicate)Implements io. Drops elements while the predicate holds for the current element. |
| public static < Component type of Nil, determined by type inference in the particular context. T> Stream | |
| public default Stream | Returns: newStream composed from this stream extended with a Stream of provided valuevalue with which the stream should be extended next)Extends (continues) this |
| public default Stream | |
| public default Stream | Returns: newStream composed from this stream extended with values calculated by the provided functiona function which calculates the next value basing on the previous value nextFunction)Extends (continues) this |
| public static < Component type of the Stream T> Stream | |
| public static < Component type of the Stream T> Stream | Returns: A Stream of sizen, where each element is the given element.The number of elements in the Stream n, T The element element)Returns a Stream containing |
| public default Stream | filter(Predicate<? super T>
A predicate predicate)Implements io. Returns a new traversable consisting of all elements which satisfy the given predicate. |
| public default <U> Stream | flatMap(Function<? super T, ? extends Iterable<? extends U>>
A mapper mapper)Implements io. FlatMaps this Traversable. |
| public static Stream | Returns: a new Stream of int values starting fromfroma start int value value)Returns an infinitely long Stream of |
| public static Stream | Returns: a newStream of int values starting from froma start int value value, int the step by which to advance on each next value step)Returns an infinite long Stream of |
| public static Stream | Returns: a new Stream of long values starting fromfroma start long value value)Returns an infinitely long Stream of |
| public static Stream | Returns: a newStream of long values starting from froma start long value value, long the step by which to advance on each next value step)Returns an infinite long Stream of |
| public default T | get(int
an index index)Implements io. Returns the element at the specified index. |
| public default <C> Map | groupBy(Function<? super T, ? extends C>
A function which classifies elements into classes classifier)Implements io. Groups this elements by classifying the elements. |
| public default Iterator | grouped(int
a positive block size size)Implements io. Groups this |
| public default boolean | hasDefiniteSize()
Implements io. Checks if this Traversable is known to have a finite size. |
| public default int | indexOf(T
an element element, int start index from)Implements io. Returns the index of the first occurrence of the given element after or at some start index or -1 if this does not contain the given element. |
| public default Stream | init()
Implements io. Dual of tail(), returning all elements except the last. |
| public default Option | initOption()
Implements io. Dual of tailOption(), returning all elements except the last as |
| public default Stream | insert(int
an index index, T an element element)Implements io. Inserts the given element at the specified index. |
| public default Stream | insertAll(int
an index index, Iterable<? extends T> An Iterable of elements elements)Implements io. Inserts the given elements at the specified index. |
| public default Stream | intersperse(T
An element. element)Implements io. Inserts an element between all elements of this Traversable. |
| public default boolean | |
| public default boolean | |
| public default boolean | isTraversableAgain()
Implements io. Checks if this Traversable can be repeatedly traversed. |
| public static < value type T> Stream | |
| public static < value type T> Stream | |
| public default T | |
| public default int | lastIndexOf(T
an element element, int the end index end)Implements io. Returns the index of the last occurrence of the given element before or at a given end index or -1 if this does not contain the given element. |
| public default Stream | leftPadTo(int
the target length length, T the padding element element)Implements io. A copy of this sequence with an element prepended until a given target length is reached. |
| public default int | length()
Implements io. Computes the number of elements of this Traversable. |
| public default <U> Stream | map(Function<? super T, ? extends U>
A mapper. mapper)Implements io. Maps the elements of this |
| public static < Component type of the T> StreamStream. | |
| public static < The component type T> Stream | Returns: A new Stream instance containing the given elementAn element. element)Returns a singleton |
| public static < Component type of the Stream. T> Stream | Returns: A list containing the given elements in the same order.Zero or more elements. elements)Creates a Stream of the given elements. |
| public static < Component type of the Stream. T> Stream | |
| public static < Component type of the Stream. T> Stream | Returns: A Stream containing the given elements in the same order.Creates a Stream that contains the elements of the given |
| public static Stream | Returns: A new Stream of Boolean valuesboolean values elements)Creates a Stream from boolean values. |
| public static Stream | Returns: A new Stream of Byte valuesbyte values elements)Creates a Stream from byte values. |
| public static Stream | Returns: A new Stream of Character valueschar values elements)Creates a Stream from char values. |
| public static Stream | Returns: A new Stream of Double valuesdouble values elements)Creates a Stream values double values. |
| public static Stream | Returns: A new Stream of Float valuesfloat values elements)Creates a Stream from float values. |
| public static Stream | Returns: A new Stream of Integer valuesint values elements)Creates a Stream from int values. |
| public static Stream | Returns: A new Stream of Long valueslong values elements)Creates a Stream from long values. |
| public static Stream | Returns: A new Stream of Short valuesshort values elements)Creates a Stream from short values. |
| public default Stream | orElse(Iterable<? extends T>
An alternative other)TraversableImplements io. Returns this |
| public default Stream | orElse(Supplier<? extends Iterable<? extends T>>
An alternative supplier)Traversable supplierImplements io. Returns this |
| public default Stream | padTo(int
the target length length, T the padding element element)Implements io. A copy of this sequence with an element appended until a given target length is reached. |
| public default Tuple2 | partition(Predicate<? super T>
A predicate which classifies an element if it is in the first or the second traversable. predicate)Implements io. Creates a partition of this |
| public default Stream | patch(int
the index of the first replaced element from, Iterable<? extends T> sequence for replacement that, int the number of elements to drop in the original list replaced)Implements io. Produces a new list where a slice of elements in this list is replaced by another sequence. |
| public default Stream | peek(Consumer<? super T>
The action that will be performed on the element(s). action)Implements io. Performs the given |
| public default Stream | permutations()
Implements io. Computes all unique permutations. |
| public default Stream | prepend(T
An element element)Implements io. Prepends an element to this. |
| public default Stream | prependAll(Iterable<? extends T>
An Iterable of elements elements)Implements io. Prepends all given elements to this. |
| public static Stream | |
| public static Stream | Returns: a range of int values as specified orNil if from >= toExclusivethe first number from, int the last number + 1 toExclusive)Creates a Stream of int numbers starting from |
| public static Stream | Returns: a range of long values as specified orNil if from >= toExclusivethe first number from, long the last number + 1 toExclusive)Creates a Stream of long numbers starting from |
| public static Stream | |
| public static Stream | |
| public static Stream | Returns: a range of long values as specified orNil iffrom >= toInclusive and step > 0 orfrom <= toInclusive and step < 0the first number from, int the last number + 1 toExclusive, int the step step)Creates a Stream of int numbers starting from |
| public static Stream | Returns: a range of long values as specified orNil iffrom >= toInclusive and step > 0 orfrom <= toInclusive and step < 0the first number from, long the last number + 1 toExclusive, long the step step)Creates a Stream of long numbers starting from |
| public static Stream | |
| public static Stream | Returns: a range of int values as specified orNil if from > toInclusivethe first number from, int the last number toInclusive)Creates a Stream of int numbers starting from |
| public static Stream | Returns: a range of long values as specified orNil if from > toInclusivethe first number from, long the last number toInclusive)Creates a Stream of long numbers starting from |
| public static Stream | |
| public static Stream | |
| public static Stream | Returns: a range of int values as specified orNil iffrom > toInclusive and step > 0 orfrom < toInclusive and step < 0the first number from, int the last number toInclusive, int the step step)Creates a Stream of int numbers starting from |
| public static Stream | Returns: a range of int values as specified orNil iffrom > toInclusive and step > 0 orfrom < toInclusive and step < 0the first number from, long the last number toInclusive, long the step step)Creates a Stream of long numbers starting from |
| public default Stream | reject(Predicate<? super T>
A predicate predicate)Implements io. Returns a new traversable consisting of all elements which do not satisfy the given predicate. |
| public default Stream | remove(T
An element to be removed from this Seq. element)Implements io. Removes the first occurrence of the given element. |
| public default Stream | removeAll(T
An element to be removed from this Seq. element)Implements io. Removes all occurrences of the given element. |
| public default Stream | removeAll(Iterable<? extends T>
Elements to be removed from this Seq. elements)Implements io. Removes all occurrences of the given elements. |
| public default Stream | removeAll(Predicate<? super T>
the predicate used to test elements predicate)Implements io.
Deprecated
Returns a new Seq consisting of all elements which do not satisfy the given predicate. |
| public default Stream | removeAt(int
position of element to remove index)Implements io. Removes the element at the specified position in this sequence. |
| public default Stream | removeFirst(Predicate<T>
an predicate predicate)Implements io. Removes the first occurrence that satisfy predicate |
| public default Stream | removeLast(Predicate<T>
an predicate predicate)Implements io. Removes the last occurrence that satisfy predicate |
| public default Stream | replace(T
An element to be substituted. currentElement, T A replacement for currentElement. newElement)Implements io. Replaces the first occurrence (if exists) of the given currentElement with newElement. |
| public default Stream | replaceAll(T
An element to be substituted. currentElement, T A replacement for currentElement. newElement)Implements io. Replaces all occurrences of the given currentElement with newElement. |
| public default Stream | retainAll(Iterable<? extends T>
Elements to be kept. elements)Implements io. Keeps all occurrences of the given elements from this. |
| public default Stream | |
| public default Stream | rotateLeft(int
distance of left rotation n)Implements io. Circular rotates the elements by the specified distance to the left direction. |
| public default Stream | rotateRight(int
distance of right rotation n)Implements io. Circular rotates the elements by the specified distance to the right direction. |
| public default Stream | scan(T
neutral element for the operator op zero, BiFunction<? super T, ? super T, ? extends T> the associative operator for the scan operation)Implements io. Computes a prefix scan of the elements of the collection. |
| public default <U> Stream | scanLeft(U
the initial value zero, BiFunction<? super U, ? super T, ? extends U> the binary operator applied to the intermediate result and the element operation)Implements io. Produces a collection containing cumulative results of applying the operator going left to right. |
| public default <U> Stream | scanRight(U
the initial value zero, BiFunction<? super T, ? super U, ? extends U> the binary operator applied to the intermediate result and the element operation)Implements io. Produces a collection containing cumulative results of applying the operator going right to left. |
| public default Stream | shuffle()
Implements io. Randomize the order of the elements in the current sequence. |
| public default Stream | slice(int
the beginning index, inclusive beginIndex, int the end index, exclusive endIndex)Implements io. Returns a Seq that is a slice of this. |
| public default Iterator | slideBy(Function<? super T, ?>
A function which classifies elements into classes classifier)Implements io. Slides a non-overlapping window of a variable size over this |
| public default Iterator | sliding(int
a positive window size size)Implements io. Slides a window of a specific |
| public default Iterator | sliding(int
a positive window size size, int a positive step size step)Implements io. Slides a window of a specific |
| public default <U extends Comparable | sortBy(Function<? super T, ? extends U>
A mapper mapper)Implements io. Sorts this elements by comparing the elements in a different domain, using the given |
| public default <U> Stream | sortBy(Comparator<? super U>
A comparator comparator, Function<? super T, ? extends U> A mapper mapper)Implements io. Sorts this elements by comparing the elements in a different domain, using the given |
| public default Stream | sorted()
Implements io. Sorts this elements according to their natural order. |
| public default Stream | sorted(Comparator<? super T>
A comparator comparator)Implements io. Sorts this elements according to the provided |
| public default Tuple2 | span(Predicate<? super T>
A predicate. predicate)Implements io. Returns a tuple where the first element is the longest prefix of elements that satisfy the given
|
| public default Tuple2 | splitAt(int
An index. n)Implements io. Splits a Seq at the specified index. |
| public default Tuple2 | splitAt(Predicate<? super T>
An predicate predicate)Implements io. Splits a sequence at the first element which satisfies the |
| public default Tuple2 | splitAtInclusive(Predicate<? super T>
An predicate predicate)Implements io. Splits a sequence at the first element which satisfies the |
| public default String | stringPrefix()
Implements io. Returns the name of this Value type, which is used by toString(). |
| public default Stream | subSequence(int
the beginning index, inclusive beginIndex)Implements io. Returns a Seq that is a subsequence of this. |
| public default Stream | subSequence(int
the beginning index, inclusive beginIndex, int the end index, exclusive endIndex)Implements io. Returns a Seq that is a subsequence of this. |
| public static < Component type of the Stream T> Stream | Returns: A Stream consisting of elementsf(0),f(1), ..., f(n - 1)The number of elements in the Stream n, Function<? super Integer, ? extends T> The Function computing element values f)Returns a Stream containing |
| public Stream | tail()
Redeclares io. Drops the first element of a non-empty Traversable. |
| public default Option | tailOption()
Implements io. Drops the first element of a non-empty Traversable and returns an |
| public default Stream | take(int
The number of elements to take. n)Implements io. Takes the first n elements of this or all elements, if this length < n. |
| public default Stream | takeRight(int
The number of elements to take. n)Implements io. Takes the last n elements of this or all elements, if this length < n. |
| public default Stream | takeRightUntil(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Takes elements until the predicate holds for the current element, starting from the end. |
| public default Stream | takeRightWhile(Predicate<? super T>
A condition tested subsequently for this elements, starting from the end. predicate)Implements io. Takes elements while the predicate holds for the current element, starting from the end. |
| public default Stream | takeUntil(Predicate<? super T>
A condition tested subsequently for this elements. predicate)Implements io. Takes elements until the predicate holds for the current element. |
| public default Stream | takeWhile(Predicate<? super T>
A condition tested subsequently for the contained elements. predicate)Implements io. Takes elements while the predicate holds for the current element. |
| public default < Type of transformation result U> U | |
| public static < matrix element type T> Stream | |
| public static < type of seeds and unfolded values T> Stream | |
| public static < type of seeds T, type of unfolded values U> Stream | Returns: a Stream with the values built up by the iterationthe start value for the iteration seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> the function to get the next step of the iteration f)Creates a Stream from a seed value and a function. |
| public static < type of seeds T, type of unfolded values U> Stream | Returns: a Stream with the values built up by the iterationthe start value for the iteration seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> the function to get the next step of the iteration f)Creates a Stream from a seed value and a function. |
| public default <T1, T2> Tuple2 | unzip(Function<? super T, Tuple2<? extends T1, ? extends T2>>
a function which converts elements of this to pairs unzipper)Implements io. Unzips this elements by mapping this elements to pairs which are subsequently split into two distinct sets. |
| public default <T1, T2, T3> Tuple3 | unzip3(Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>>
a function which converts elements of this to pairs unzipper)Implements io. Unzips this elements by mapping this elements to triples which are subsequently split into three distinct sets. |
| public default Stream | update(int
an index index, T an element element)Implements io. Updates the given element at the specified index. |
| public default Stream | update(int
an index index, Function<? super T, ? extends T> a function transforming the previous value updater)Implements io. Updates the given element at the specified index using the specified function. |
| public default <U> Stream | zip(Iterable<? extends U>
The Iterable providing the second half of each result pair. that)Implements io. Returns a traversable formed from this traversable and another Iterable collection by combining corresponding elements in pairs. |
| public default <U> Stream | zipAll(Iterable<? extends U>
The Iterable providing the second half of each result pair. iterable, T The element to be used to fill up the result if this traversable is shorter than that. thisElem, U The element to be used to fill up the result if that is shorter than this traversable. thatElem)Implements io. Returns a traversable formed from this traversable and another Iterable by combining corresponding elements in pairs. |
| public default <U, R> Stream | zipWith(Iterable<? extends U>
The Iterable providing the second parameter of the mapper. that, BiFunction<? super T, ? super U, ? extends R> a mapper. mapper)Implements io. Returns a traversable formed from this traversable and another Iterable collection by mapping elements. |
| public default Stream | zipWithIndex()
Implements io. Zips this traversable with its indices. |
| public default <U> Stream | zipWithIndex(BiFunction<? super T, ? super Integer, ? extends U>
a mapper. mapper)Implements io. Zips this traversable with its indices by applying mapper provided. |