Skip to content
Buy me a coffee

Stream

Defined in: src/stream/Streams.ts:35

A lazy, single-use sequence of elements supporting aggregate operations: intermediate operations build the pipeline, one terminal operation runs it. The Stream value holds the factories (Stream.of, Stream.from, …).

  • Stream<T>

T

[dispose](): void

Defined in: src/stream/Stream.ts:71

Calls close(), so using s = ... closes the stream at the end of the block. Present where the runtime has Symbol.dispose.

void

StreamType.[dispose]


[iterator](): Iterator<T, any, any>

Defined in: site/node_modules/typescript/lib/lib.es2015.iterable.d.ts:47

Iterator<T, any, any>

StreamType.[iterator]


allMatch(predicate): boolean

Defined in: src/stream/Stream.ts:290

Returns whether all elements match predicate; true for an empty stream. Terminal, short-circuiting.

(t) => boolean

boolean

StreamType.allMatch


anyMatch(predicate): boolean

Defined in: src/stream/Stream.ts:288

Returns whether any element matches predicate; false for an empty stream. Terminal, short-circuiting.

(t) => boolean

boolean

StreamType.anyMatch


close(): void

Defined in: src/stream/Stream.ts:69

Closes the stream and runs its close handlers once, in registration order. If handlers throw, the rest still run and the first error is rethrown with later ones attached via suppressed. A terminal operation does not close the stream; closing a closed stream does nothing.

void

StreamType.close


collect<R, A>(collector): R

Defined in: src/stream/Stream.ts:260

Performs a mutable reduction with collector (see Collectors) and returns its result. Terminal.

R

A

Collector<T, A, R>

R

StreamType.collect

collect<R>(supplier, accumulator, combiner): R

Defined in: src/stream/Stream.ts:266

Performs a mutable reduction: supplier creates the container, accumulator adds each element to it. Terminal. combiner merges the second container into the first; evaluation is sequential so it is never called.

R

() => R

(r, t) => void

(a, b) => void

R

the container

StreamType.collect


count(): number

Defined in: src/stream/Stream.ts:286

Returns the number of elements. Terminal. When the pipeline’s size is known exactly (a sized source and no filtering stage) the result is computed without traversal, so peek and map callbacks do not run.

number

StreamType.count


distinct(): Stream<T>

Defined in: src/stream/Stream.ts:188

Returns a stream without duplicates, by equals()/hashCode() like a HashSet (primitives by value, plain objects by identity); the first occurrence is kept. Intermediate and stateful (it remembers every element seen) but not a barrier: each element is emitted the first time it is seen.

Stream<T>

StreamType.distinct


dropWhile(predicate): Stream<T>

Defined in: src/stream/Stream.ts:219

Returns the elements after the longest prefix matching predicate. Intermediate, stateful.

(t) => boolean

Stream<T>

StreamType.dropWhile


filter(predicate): Stream<T>

Defined in: src/stream/Stream.ts:151

Returns a stream of the elements that match predicate. Intermediate, stateless.

(t) => boolean

Stream<T>

StreamType.filter


findAny(): Optional<T>

Defined in: src/stream/Stream.ts:303

Returns some element, or an empty Optional. Terminal, short-circuiting. Evaluation is sequential, so it is always the first element, as findFirst() returns.

Optional<T>

NullPointerException if the element selected is null

StreamType.findAny


findFirst(): Optional<T>

Defined in: src/stream/Stream.ts:297

Returns the first element, or an empty Optional for an empty stream. Terminal, short-circuiting.

Optional<T>

NullPointerException if the element selected is null

StreamType.findFirst


flatMap<R>(mapper): Stream<R>

Defined in: src/stream/Stream.ts:165

Returns a stream that replaces each element with the elements of the stream mapper returns. Intermediate. A null/undefined result counts as an empty stream; each mapped stream is closed after its elements have been pushed downstream, even when a short-circuit stops the pipeline early.

R

(t) => Stream<R> | null | undefined

Stream<R>

StreamType.flatMap


flatMapToDouble(mapper): DoubleStream

Defined in: src/stream/Stream.ts:171

Like flatMap, with a mapper that returns a DoubleStream; null/undefined counts as empty, each mapped stream is closed.

(t) => DoubleStream | null | undefined

DoubleStream

StreamType.flatMapToDouble


flatMapToInt(mapper): IntStream

Defined in: src/stream/Stream.ts:167

Like flatMap, with a mapper that returns an IntStream; null/undefined counts as empty, each mapped stream is closed.

(t) => IntStream | null | undefined

IntStream

StreamType.flatMapToInt


flatMapToLong(mapper): LongStream

Defined in: src/stream/Stream.ts:169

Like flatMap, with a mapper that returns a LongStream; null/undefined counts as empty, each mapped stream is closed.

(t) => LongStream | null | undefined

LongStream

StreamType.flatMapToLong


forEach(action): void

Defined in: src/stream/Stream.ts:228

Runs action for each element. Terminal; evaluation is sequential, so elements arrive in encounter order.

(t) => void

void

StreamType.forEach


forEachOrdered(action): void

Defined in: src/stream/Stream.ts:230

Runs action for each element in encounter order. Terminal; here the same as forEach.

(t) => void

void

StreamType.forEachOrdered


gather<R>(gatherer): Stream<R>

Defined in: src/stream/Stream.ts:224

Returns a stream transformed by gatherer, a custom intermediate operation (see Gatherers). It short-circuits when the gatherer’s integrator returns false; the gatherer’s finisher runs at the end.

R

Gatherer<T, any, R>

Stream<R>

StreamType.gather


isParallel(): boolean

Defined in: src/stream/Stream.ts:44

Returns true if parallel() was requested. It only reports the flag; evaluation is always sequential.

boolean

StreamType.isParallel


iterator(): Iterator<T>

Defined in: src/stream/Stream.ts:35

Returns an Iterator over the stream’s elements. This is a terminal operation; elements are pulled through the stages lazily, as hasNext()/next() ask for them. Unlike for…of, it does not close the stream.

Iterator<T>

IllegalStateException if the stream was already operated upon or closed

StreamType.iterator


limit(maxSize): Stream<T>

Defined in: src/stream/Stream.ts:210

Returns a stream of at most the first maxSize elements. Intermediate, stateful and short-circuiting.

number

Stream<T>

IllegalArgumentException if maxSize is negative or not an integer

StreamType.limit


map<R>(mapper): Stream<R>

Defined in: src/stream/Stream.ts:153

Returns a stream of mapper applied to each element. Intermediate, stateless; a null result flows through like any element.

R

(t) => R

Stream<R>

StreamType.map


mapMulti<R>(mapper): Stream<R>

Defined in: src/stream/Stream.ts:176

Returns a stream of the values mapper passes to consumer, zero or more per element. Intermediate, stateless. A push-style flatMap that creates no inner streams.

R

(t, consumer) => void

Stream<R>

StreamType.mapMulti


mapMultiToDouble(mapper): DoubleStream

Defined in: src/stream/Stream.ts:182

Like mapMulti, producing a DoubleStream.

(t, consumer) => void

DoubleStream

StreamType.mapMultiToDouble


mapMultiToInt(mapper): IntStream

Defined in: src/stream/Stream.ts:178

Like mapMulti, producing an IntStream.

(t, consumer) => void

IntStream

StreamType.mapMultiToInt


mapMultiToLong(mapper): LongStream

Defined in: src/stream/Stream.ts:180

Like mapMulti, producing a LongStream.

(t, consumer) => void

LongStream

StreamType.mapMultiToLong


mapToDouble(mapper): DoubleStream

Defined in: src/stream/Stream.ts:159

Returns a DoubleStream of mapper applied to each element. Intermediate, stateless.

(t) => number

DoubleStream

StreamType.mapToDouble


mapToInt(mapper): IntStream

Defined in: src/stream/Stream.ts:155

Returns an IntStream of mapper applied to each element. Intermediate, stateless.

(t) => number

IntStream

StreamType.mapToInt


mapToLong(mapper): LongStream

Defined in: src/stream/Stream.ts:157

Returns a LongStream of mapper applied to each element. Intermediate, stateless.

(t) => number

LongStream

StreamType.mapToLong


max(comparator): Optional<T>

Defined in: src/stream/Stream.ts:281

Returns the largest element by comparator (the first of equal ones), or an empty Optional. Terminal.

Comparator<T>

Optional<T>

NullPointerException if the element selected is null

StreamType.max


min(comparator): Optional<T>

Defined in: src/stream/Stream.ts:276

Returns the smallest element by comparator (the first of equal ones), or an empty Optional. Terminal.

Comparator<T>

Optional<T>

NullPointerException if the element selected is null

StreamType.min


noneMatch(predicate): boolean

Defined in: src/stream/Stream.ts:292

Returns whether no element matches predicate; true for an empty stream. Terminal, short-circuiting.

(t) => boolean

boolean

StreamType.noneMatch


onClose(closeHandler): Stream

Defined in: src/stream/Stream.ts:63

Registers a handler run by close() and returns this same stream; handlers run in registration order.

() => void

Stream

IllegalStateException if the stream was already operated upon or closed

NullPointerException if closeHandler is null

StreamType.onClose


parallel(): Stream

Defined in: src/stream/Stream.ts:51

Sets the parallel flag and returns this same stream. Evaluation stays sequential on the single JS thread; code written for parallel streams (associative reducers, combiners, findAny) gives the same results.

Stream

StreamType.parallel


peek(action): Stream<T>

Defined in: src/stream/Stream.ts:205

Returns a stream that runs action on each element as it passes. Intermediate, stateless; meant for debugging. Elements that are never pulled (after a short-circuit, or when count() skips traversal) are not seen.

(t) => void

Stream<T>

StreamType.peek


reduce(identity, accumulator): T

Defined in: src/stream/Stream.ts:246

Folds the elements, starting from identity, with an associative accumulator. Terminal.

T

(a, b) => T

T

the result, or identity for an empty stream

StreamType.reduce

reduce(accumulator): Optional<T>

Defined in: src/stream/Stream.ts:252

Folds the elements with an associative accumulator, without an initial value. Terminal.

(a, b) => T

Optional<T>

the result, or an empty Optional for an empty stream

NullPointerException if the result is null

StreamType.reduce

reduce<U>(identity, accumulator, combiner): U

Defined in: src/stream/Stream.ts:258

Folds the elements into a value of another type U, starting from identity. Terminal. combiner merges two partial results; evaluation is sequential so it is never called, but it must be correct.

U

U

(u, t) => U

(a, b) => U

U

the result, or identity for an empty stream

StreamType.reduce


sequential(): Stream

Defined in: src/stream/Stream.ts:46

Clears the parallel flag and returns this same stream. Allowed on a stream that was already used.

Stream

StreamType.sequential


skip(n): Stream<T>

Defined in: src/stream/Stream.ts:215

Returns a stream without the first n elements (this same stream when n is 0). Intermediate, stateful.

number

Stream<T>

IllegalArgumentException if n is negative or not an integer

StreamType.skip


sorted(): Stream<T>

Defined in: src/stream/Stream.ts:195

Returns a stream sorted in natural order (numbers numerically, strings by code unit, objects through compareTo); stable for ordered streams. Intermediate and a stateful barrier: it buffers every element before emitting any, so it never finishes on an infinite stream. A no-op on a source already in natural order.

Stream<T>

ClassCastException (from the terminal operation) for elements that cannot be compared

StreamType.sorted

sorted(comparator): Stream<T>

Defined in: src/stream/Stream.ts:200

Returns a stream sorted by comparator; stable for ordered streams. Intermediate and a stateful barrier.

Comparator<T>

Stream<T>

NullPointerException if comparator is null or undefined

StreamType.sorted


spliterator(): Spliterator<T>

Defined in: src/stream/Stream.ts:42

Returns a Spliterator over the stream’s elements; a terminal operation that pulls elements lazily. It reports the pipeline’s characteristics (ORDERED, DISTINCT, SORTED, SIZED / SUBSIZED as the stages keep them); with no stages it is the source spliterator itself.

Spliterator<T>

IllegalStateException if the stream was already operated upon or closed

StreamType.spliterator


takeWhile(predicate): Stream<T>

Defined in: src/stream/Stream.ts:217

Returns the longest prefix of elements matching predicate. Intermediate, stateful and short-circuiting: it stops at the first failure.

(t) => boolean

Stream<T>

StreamType.takeWhile


toArray(): T[]

Defined in: src/stream/Stream.ts:232

Returns a new JS array of the elements in encounter order. Terminal.

T[]

StreamType.toArray

toArray<A>(generator): A

Defined in: src/stream/Stream.ts:241

Returns the elements in an array-like that generator creates (a typed array, for example). Terminal. When the stream size is known exactly, generator(size) is called before the traversal and must return an array of exactly that length. Otherwise it is called with the element count afterwards, and slots past the elements are set to null. Element type checks of the reference are not emulated.

A extends WritableArrayLike<T>

(length) => A

A

IllegalStateException if a sized stream’s generator returns an array of another length

IndexOutOfBoundsException if an unsized stream’s generator returns a shorter array

StreamType.toArray


toList(): List<T>

Defined in: src/stream/Stream.ts:271

Returns an unmodifiable List of the elements in encounter order. Terminal. Unlike List.of and Collectors.toUnmodifiableList() it allows null elements; mutators throw UnsupportedOperationException.

List<T>

StreamType.toList


unordered(): Stream

Defined in: src/stream/Stream.ts:57

Returns an equivalent stream without the encounter-order constraint (an intermediate operation), or this stream itself when it is already unordered. Evaluation is sequential, so elements still arrive in source order.

Stream

IllegalStateException if the stream was already operated upon or closed

StreamType.unordered