> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/types/stream.md).

# stream

In BoxLang, the `stream` type is represented by the native Java class `java.util.stream.Stream`. The member functions below are provided by the BoxLang runtime and can be called directly on the value, in addition to the methods of the underlying Java class.

A sequence of elements supporting sequential and parallel aggregate operations.

## Stream Methods

<details>

<summary><code>toBXArray()</code></summary>

Collect a Java stream into a BoxLang Array

### Method Signature

```
toBXArray()
```

### Arguments

This function does not accept any arguments

</details>

<details>

<summary><code>toBXList(delimiter=[string])</code></summary>

Collect a Java stream into a BoxLang delimited list.

Each item in the stream will cast to a string and then be joined with the delimiter.

### Method Signature

```
toBXList(delimiter=[string])
```

### Arguments

| Argument    | Type     | Required | Description           | Default |
| ----------- | -------- | -------- | --------------------- | ------- |
| `delimiter` | `string` | `false`  | The delimiter to use. | `,`     |

</details>

<details>

<summary><code>toBXQuery(query=[query])</code></summary>

Collect a Java stream into a BoxLang Query.

Provide a template query to match the columns and types of the stream. Once the stream collects, it will return a Query object that can be used in BoxLang.

## Usage

```
 // Create a query template
 templateQuery = queryNew( "id,name,age" );
 // Create a stream from an array of structs
 stream = [ {id:1, name:"John", age:30}, {id:2, name:"Jane", age:25} ].toStream();
 // Convert the stream to a query
 result = stream.toBXQuery( templateQuery );
 
```

### Method Signature

```
toBXQuery(query=[query])
```

### Arguments

| Argument | Type    | Required | Description                                                           | Default |
| -------- | ------- | -------- | --------------------------------------------------------------------- | ------- |
| `query`  | `query` | `true`   | The query template that defines the structure of the resulting Query. |         |

</details>

<details>

<summary><code>toBXStruct(type=[string])</code></summary>

Collect a Java stream into a BoxLang Struct.

Must be a stream of Map.Entry instances.

### Method Signature

```
toBXStruct(type=[string])
```

### Arguments

| Argument | Type     | Required | Description                   | Default   |
| -------- | -------- | -------- | ----------------------------- | --------- |
| `type`   | `string` | `false`  | The type of struct to create. | `default` |

</details>

## Java Methods

> **Use at your own risk:** These are the public methods of the native Java class `java.util.stream.Stream`, documented from the JDK 21 javadocs. They are not part of the BoxLang API, are not tested or supported by BoxLang, and may change between Java versions.

<details>

<summary><code>boolean allMatch(Predicate&#x3C;? super T> predicate)</code></summary>

Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then `true` is returned and the predicate is not evaluated.

This is a short-circuiting terminal operation.

**Parameters:**

* `predicate` - a non-interfering, stateless predicate to apply to elements of this stream

**Returns:** `true` if either all elements of the stream match the provided predicate or the stream is empty, otherwise `false`

</details>

<details>

<summary><code>boolean anyMatch(Predicate&#x3C;? super T> predicate)</code></summary>

Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then `false` is returned and the predicate is not evaluated.

This is a short-circuiting terminal operation.

**Parameters:**

* `predicate` - a non-interfering, stateless predicate to apply to elements of this stream

**Returns:** `true` if any elements of the stream match the provided predicate, otherwise `false`

</details>

<details>

<summary><code>static &#x3C;T> Builder&#x3C;T> builder()</code></summary>

Returns a builder for a `Stream`.

**Returns:** a stream builder

</details>

<details>

<summary><code>&#x3C;R, A> R collect(Collector&#x3C;? super T, A, R> collector)</code></summary>

Performs a mutable reduction operation on the elements of this stream using a `Collector`. A `Collector` encapsulates the functions used as arguments to `BiConsumer, BiConsumer)`, allowing for reuse of collection strategies and composition of collect operations such as multiple-level grouping or partitioning.

If the stream is parallel, and the `Collector` is `concurrent`, and either the stream is unordered or the collector is `unordered`, then a concurrent reduction will be performed (see `Collector` for details on concurrent reduction.)

This is a terminal operation.

When executed in parallel, multiple intermediate results may be instantiated, populated, and merged so as to maintain isolation of mutable data structures. Therefore, even when executed in parallel with non-thread-safe data structures (such as `ArrayList`), no additional synchronization is needed for a parallel reduction.

**Parameters:**

* `collector` - the `Collector` describing the reduction

**Returns:** the result of the reduction

</details>

<details>

<summary><code>&#x3C;R> R collect(Supplier&#x3C;R> supplier, BiConsumer&#x3C;R, ? super T> accumulator, BiConsumer&#x3C;R, R> combiner)</code></summary>

Performs a mutable reduction operation on the elements of this stream. A mutable reduction is one in which the reduced value is a mutable result container, such as an `ArrayList`, and elements are incorporated by updating the state of the result rather than by replacing the result. This produces a result equivalent to:

```
R result = supplier.get();
     for (T element : this stream)
         accumulator.accept(result, element);
     return result;
```

Like `BinaryOperator)`, `collect` operations can be parallelized without requiring additional synchronization.

This is a terminal operation.

**Parameters:**

* `supplier` - a function that creates a new mutable result container. For a parallel execution, this function may be called multiple times and must return a fresh value each time.
* `accumulator` - an associative, non-interfering, stateless function that must fold an element into a result container.
* `combiner` - an associative, non-interfering, stateless function that accepts two partial result containers and merges them, which must be compatible with the accumulator function. The combiner function must fold the elements from the second result container into the first result container.

**Returns:** the result of the reduction

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> concat(Stream&#x3C;? extends T> a, Stream&#x3C;? extends T> b)</code></summary>

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel. When the resulting stream is closed, the close handlers for both input streams are invoked.

This method operates on the two input streams and binds each stream to its source. As a result subsequent modifications to an input stream source may not be reflected in the concatenated stream result.

**Parameters:**

* `a` - the first stream
* `b` - the second stream

**Returns:** the concatenation of the two input streams

</details>

<details>

<summary><code>long count()</code></summary>

Returns the count of elements in this stream. This is a special case of a reduction and is equivalent to:

```
return mapToLong(e -> 1L).sum();
```

This is a terminal operation.

**Returns:** the count of elements in this stream

</details>

<details>

<summary><code>Stream&#x3C;T> distinct()</code></summary>

Returns a stream consisting of the distinct elements (according to `Object#equals(Object)`) of this stream.

For ordered streams, the selection of distinct elements is stable (for duplicated elements, the element appearing first in the encounter order is preserved.) For unordered streams, no stability guarantees are made.

This is a stateful intermediate operation.

**Returns:** the new stream

</details>

<details>

<summary><code>Stream&#x3C;T> dropWhile(Predicate&#x3C;? super T> predicate)</code></summary>

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate. Otherwise returns, if this stream is unordered, a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate.

If this stream is ordered then the longest prefix is a contiguous sequence of elements of this stream that match the given predicate. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate.

If this stream is unordered, and some (but not all) elements of this stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to drop any subset of matching elements (which includes the empty set).

Independent of whether this stream is ordered or unordered if all elements of this stream match the given predicate then this operation drops all elements (the result is an empty stream), or if no elements of the stream match the given predicate then no elements are dropped (the result is the same as the input).

This is a stateful intermediate operation.

**Parameters:**

* `predicate` - a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.

**Returns:** the new stream

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> empty()</code></summary>

Returns an empty sequential `Stream`.

**Returns:** an empty sequential stream

</details>

<details>

<summary><code>Stream&#x3C;T> filter(Predicate&#x3C;? super T> predicate)</code></summary>

Returns a stream consisting of the elements of this stream that match the given predicate.

This is an intermediate operation.

**Parameters:**

* `predicate` - a non-interfering, stateless predicate to apply to each element to determine if it should be included

**Returns:** the new stream

</details>

<details>

<summary><code>Optional&#x3C;T> findAny()</code></summary>

Returns an `Optional` describing some element of the stream, or an empty `Optional` if the stream is empty.

This is a short-circuiting terminal operation.

The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations on the same source may not return the same result. (If a stable result is desired, use `#findFirst()` instead.)

**Returns:** an `Optional` describing some element of this stream, or an empty `Optional` if the stream is empty

</details>

<details>

<summary><code>Optional&#x3C;T> findFirst()</code></summary>

Returns an `Optional` describing the first element of this stream, or an empty `Optional` if the stream is empty. If the stream has no encounter order, then any element may be returned.

This is a short-circuiting terminal operation.

**Returns:** an `Optional` describing the first element of this stream, or an empty `Optional` if the stream is empty

</details>

<details>

<summary><code>&#x3C;R> Stream&#x3C;R> flatMap(Function&#x3C;? super T, ? extends Stream&#x3C;? extends R>> mapper)</code></summary>

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is `closed` after its contents have been placed into this stream. (If a mapped stream is `null` an empty stream is used, instead.)

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element which produces a stream of new values

**Returns:** the new stream

</details>

<details>

<summary><code>DoubleStream flatMapToDouble(Function&#x3C;? super T, ? extends DoubleStream> mapper)</code></summary>

Returns an `DoubleStream` consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is `closed` after its contents have placed been into this stream. (If a mapped stream is `null` an empty stream is used, instead.)

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element which produces a stream of new values

**Returns:** the new stream

</details>

<details>

<summary><code>IntStream flatMapToInt(Function&#x3C;? super T, ? extends IntStream> mapper)</code></summary>

Returns an `IntStream` consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is `closed` after its contents have been placed into this stream. (If a mapped stream is `null` an empty stream is used, instead.)

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element which produces a stream of new values

**Returns:** the new stream

</details>

<details>

<summary><code>LongStream flatMapToLong(Function&#x3C;? super T, ? extends LongStream> mapper)</code></summary>

Returns an `LongStream` consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is `closed` after its contents have been placed into this stream. (If a mapped stream is `null` an empty stream is used, instead.)

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element which produces a stream of new values

**Returns:** the new stream

</details>

<details>

<summary><code>void forEach(Consumer&#x3C;? super T> action)</code></summary>

Performs an action for each element of this stream.

This is a terminal operation.

The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does *not* guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. For any given element, the action may be performed at whatever time and in whatever thread the library chooses. If the action accesses shared state, it is responsible for providing the required synchronization.

**Parameters:**

* `action` - a non-interfering action to perform on the elements

</details>

<details>

<summary><code>void forEachOrdered(Consumer&#x3C;? super T> action)</code></summary>

Performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.

This is a terminal operation.

This operation processes the elements one at a time, in encounter order if one exists. Performing the action for one element *happens-before* performing the action for subsequent elements, but for any given element, the action may be performed in whatever thread the library chooses.

**Parameters:**

* `action` - a non-interfering action to perform on the elements

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> generate(Supplier&#x3C;? extends T> s)</code></summary>

Returns an infinite sequential unordered stream where each element is generated by the provided `Supplier`. This is suitable for generating constant streams, streams of random elements, etc.

**Parameters:**

* `s` - the `Supplier` of generated elements

**Returns:** a new infinite sequential unordered `Stream`

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> iterate(T seed, Predicate&#x3C;? super T> hasNext, UnaryOperator&#x3C;T> next)</code></summary>

Returns a sequential ordered `Stream` produced by iterative application of the given `next` function to an initial element, conditioned on satisfying the given `hasNext` predicate. The stream terminates as soon as the `hasNext` predicate returns false.

`Stream.iterate` should produce the same sequence of elements as produced by the corresponding for-loop:

```
for (T index=seed; hasNext.test(index); index = next.apply(index)) {
         ...
 }
```

The resulting sequence may be empty if the `hasNext` predicate does not hold on the seed value. Otherwise the first element will be the supplied `seed` value, the next element (if present) will be the result of applying the `next` function to the `seed` value, and so on iteratively until the `hasNext` predicate indicates that the stream should terminate.

The action of applying the `hasNext` predicate to an element *happens-before* the action of applying the `next` function to that element. The action of applying the `next` function for one element *happens-before* the action of applying the `hasNext` predicate for subsequent elements. For any given element an action may be performed in whatever thread the library chooses.

**Parameters:**

* `seed` - the initial element
* `hasNext` - a predicate to apply to elements to determine when the stream must terminate.
* `next` - a function to be applied to the previous element to produce a new element

**Returns:** a new sequential `Stream`

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> iterate(T seed, UnaryOperator&#x3C;T> f)</code></summary>

Returns an infinite sequential ordered `Stream` produced by iterative application of a function `f` to an initial element `seed`, producing a `Stream` consisting of `seed`, `f(seed)`, `f(f(seed))`, etc.

The first element (position `0`) in the `Stream` will be the provided `seed`. For `n > 0`, the element at position `n`, will be the result of applying the function `f` to the element at position `n - 1`.

The action of applying `f` for one element *happens-before* the action of applying `f` for subsequent elements. For any given element the action may be performed in whatever thread the library chooses.

**Parameters:**

* `seed` - the initial element
* `f` - a function to be applied to the previous element to produce a new element

**Returns:** a new sequential `Stream`

</details>

<details>

<summary><code>Stream&#x3C;T> limit(long maxSize)</code></summary>

Returns a stream consisting of the elements of this stream, truncated to be no longer than `maxSize` in length.

This is a short-circuiting stateful intermediate operation.

**Parameters:**

* `maxSize` - the number of elements the stream should be limited to

**Returns:** the new stream

</details>

<details>

<summary><code>&#x3C;R> Stream&#x3C;R> map(Function&#x3C;? super T, ? extends R> mapper)</code></summary>

Returns a stream consisting of the results of applying the given function to the elements of this stream.

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element

**Returns:** the new stream

</details>

<details>

<summary><code>&#x3C;R> Stream&#x3C;R> mapMulti(BiConsumer&#x3C;? super T, ? super Consumer&#x3C;R>> mapper)</code></summary>

Returns a stream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements. Replacement is performed by applying the provided mapping function to each element in conjunction with a `consumer` argument that accepts replacement elements. The mapping function calls the consumer zero or more times to provide the replacement elements.

This is an intermediate operation.

If the `consumer` argument is used outside the scope of its application to the mapping function, the results are undefined.

**Parameters:**

* `mapper` - a non-interfering, stateless function that generates replacement elements

**Returns:** the new stream

</details>

<details>

<summary><code>DoubleStream mapMultiToDouble(BiConsumer&#x3C;? super T, ? super DoubleConsumer> mapper)</code></summary>

Returns a `DoubleStream` consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements. Replacement is performed by applying the provided mapping function to each element in conjunction with a `consumer` argument that accepts replacement elements. The mapping function calls the consumer zero or more times to provide the replacement elements.

This is an intermediate operation.

If the `consumer` argument is used outside the scope of its application to the mapping function, the results are undefined.

**Parameters:**

* `mapper` - a non-interfering, stateless function that generates replacement elements

**Returns:** the new stream

</details>

<details>

<summary><code>IntStream mapMultiToInt(BiConsumer&#x3C;? super T, ? super IntConsumer> mapper)</code></summary>

Returns an `IntStream` consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements. Replacement is performed by applying the provided mapping function to each element in conjunction with a `consumer` argument that accepts replacement elements. The mapping function calls the consumer zero or more times to provide the replacement elements.

This is an intermediate operation.

If the `consumer` argument is used outside the scope of its application to the mapping function, the results are undefined.

**Parameters:**

* `mapper` - a non-interfering, stateless function that generates replacement elements

**Returns:** the new stream

</details>

<details>

<summary><code>LongStream mapMultiToLong(BiConsumer&#x3C;? super T, ? super LongConsumer> mapper)</code></summary>

Returns a `LongStream` consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements. Replacement is performed by applying the provided mapping function to each element in conjunction with a `consumer` argument that accepts replacement elements. The mapping function calls the consumer zero or more times to provide the replacement elements.

This is an intermediate operation.

If the `consumer` argument is used outside the scope of its application to the mapping function, the results are undefined.

**Parameters:**

* `mapper` - a non-interfering, stateless function that generates replacement elements

**Returns:** the new stream

</details>

<details>

<summary><code>DoubleStream mapToDouble(ToDoubleFunction&#x3C;? super T> mapper)</code></summary>

Returns a `DoubleStream` consisting of the results of applying the given function to the elements of this stream.

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element

**Returns:** the new stream

</details>

<details>

<summary><code>IntStream mapToInt(ToIntFunction&#x3C;? super T> mapper)</code></summary>

Returns an `IntStream` consisting of the results of applying the given function to the elements of this stream.

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element

**Returns:** the new stream

</details>

<details>

<summary><code>LongStream mapToLong(ToLongFunction&#x3C;? super T> mapper)</code></summary>

Returns a `LongStream` consisting of the results of applying the given function to the elements of this stream.

This is an intermediate operation.

**Parameters:**

* `mapper` - a non-interfering, stateless function to apply to each element

**Returns:** the new stream

</details>

<details>

<summary><code>Optional&#x3C;T> max(Comparator&#x3C;? super T> comparator)</code></summary>

Returns the maximum element of this stream according to the provided `Comparator`. This is a special case of a reduction.

This is a terminal operation.

**Parameters:**

* `comparator` - a non-interfering, stateless `Comparator` to compare elements of this stream

**Returns:** an `Optional` describing the maximum element of this stream, or an empty `Optional` if the stream is empty

</details>

<details>

<summary><code>Optional&#x3C;T> min(Comparator&#x3C;? super T> comparator)</code></summary>

Returns the minimum element of this stream according to the provided `Comparator`. This is a special case of a reduction.

This is a terminal operation.

**Parameters:**

* `comparator` - a non-interfering, stateless `Comparator` to compare elements of this stream

**Returns:** an `Optional` describing the minimum element of this stream, or an empty `Optional` if the stream is empty

</details>

<details>

<summary><code>boolean noneMatch(Predicate&#x3C;? super T> predicate)</code></summary>

Returns whether no elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then `true` is returned and the predicate is not evaluated.

This is a short-circuiting terminal operation.

**Parameters:**

* `predicate` - a non-interfering, stateless predicate to apply to elements of this stream

**Returns:** `true` if either no elements of the stream match the provided predicate or the stream is empty, otherwise `false`

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> of(T t)</code></summary>

Returns a sequential `Stream` containing a single element.

**Parameters:**

* `t` - the single element

**Returns:** a singleton sequential stream

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> of(T[] values)</code></summary>

Returns a sequential ordered stream whose elements are the specified values.

**Parameters:**

* `values` - the elements of the new stream

**Returns:** the new stream

</details>

<details>

<summary><code>static &#x3C;T> Stream&#x3C;T> ofNullable(T t)</code></summary>

Returns a sequential `Stream` containing a single element, if non-null, otherwise returns an empty `Stream`.

**Parameters:**

* `t` - the single element

**Returns:** a stream with a single element if the specified element is non-null, otherwise an empty stream

</details>

<details>

<summary><code>Stream&#x3C;T> peek(Consumer&#x3C;? super T> action)</code></summary>

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

This is an intermediate operation.

For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization.

**Parameters:**

* `action` - a non-interfering action to perform on the elements as they are consumed from the stream

**Returns:** the new stream

</details>

<details>

<summary><code>&#x3C;U> U reduce(U identity, BiFunction&#x3C;U, ? super T, U> accumulator, BinaryOperator&#x3C;U> combiner)</code></summary>

Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions. This is equivalent to:

```
U result = identity;
     for (T element : this stream)
         result = accumulator.apply(result, element)
     return result;
```

but is not constrained to execute sequentially.

The `identity` value must be an identity for the combiner function. This means that for all `u`, `combiner(identity, u)` is equal to `u`. Additionally, the `combiner` function must be compatible with the `accumulator` function; for all `u` and `t`, the following must hold:

```
combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
```

This is a terminal operation.

**Parameters:**

* `identity` - the identity value for the combiner function
* `accumulator` - an associative, non-interfering, stateless function for incorporating an additional element into a result
* `combiner` - an associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function

**Returns:** the result of the reduction

</details>

<details>

<summary><code>Optional&#x3C;T> reduce(BinaryOperator&#x3C;T> accumulator)</code></summary>

Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an `Optional` describing the reduced value, if any. This is equivalent to:

```
boolean foundAny = false;
     T result = null;
     for (T element : this stream) {
         if (!foundAny) {
             foundAny = true;
             result = element;
         else
             result = accumulator.apply(result, element);
     }
     return foundAny ? Optional.of(result) : Optional.empty();
 }
```

but is not constrained to execute sequentially.

The `accumulator` function must be an associative function.

This is a terminal operation.

**Parameters:**

* `accumulator` - an associative, non-interfering, stateless function for combining two values

**Returns:** an `Optional` describing the result of the reduction

</details>

<details>

<summary><code>T reduce(T identity, BinaryOperator&#x3C;T> accumulator)</code></summary>

Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value. This is equivalent to:

```
T result = identity;
     for (T element : this stream)
         result = accumulator.apply(result, element)
     return result;
```

but is not constrained to execute sequentially.

The `identity` value must be an identity for the accumulator function. This means that for all `t`, `accumulator.apply(identity, t)` is equal to `t`. The `accumulator` function must be an associative function.

This is a terminal operation.

**Parameters:**

* `identity` - the identity value for the accumulating function
* `accumulator` - an associative, non-interfering, stateless function for combining two values

**Returns:** the result of the reduction

</details>

<details>

<summary><code>Stream&#x3C;T> skip(long n)</code></summary>

Returns a stream consisting of the remaining elements of this stream after discarding the first `n` elements of the stream. If this stream contains fewer than `n` elements then an empty stream will be returned.

This is a stateful intermediate operation.

**Parameters:**

* `n` - the number of leading elements to skip

**Returns:** the new stream

</details>

<details>

<summary><code>Stream&#x3C;T> sorted()</code></summary>

Returns a stream consisting of the elements of this stream, sorted according to natural order. If the elements of this stream are not `Comparable`, a `java.lang.ClassCastException` may be thrown when the terminal operation is executed.

For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

This is a stateful intermediate operation.

**Returns:** the new stream

</details>

<details>

<summary><code>Stream&#x3C;T> sorted(Comparator&#x3C;? super T> comparator)</code></summary>

Returns a stream consisting of the elements of this stream, sorted according to the provided `Comparator`.

For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.

This is a stateful intermediate operation.

**Parameters:**

* `comparator` - a non-interfering, stateless `Comparator` to be used to compare stream elements

**Returns:** the new stream

</details>

<details>

<summary><code>Stream&#x3C;T> takeWhile(Predicate&#x3C;? super T> predicate)</code></summary>

Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate. Otherwise returns, if this stream is unordered, a stream consisting of a subset of elements taken from this stream that match the given predicate.

If this stream is ordered then the longest prefix is a contiguous sequence of elements of this stream that match the given predicate. The first element of the sequence is the first element of this stream, and the element immediately following the last element of the sequence does not match the given predicate.

If this stream is unordered, and some (but not all) elements of this stream match the given predicate, then the behavior of this operation is nondeterministic; it is free to take any subset of matching elements (which includes the empty set).

Independent of whether this stream is ordered or unordered if all elements of this stream match the given predicate then this operation takes all elements (the result is the same as the input), or if no elements of the stream match the given predicate then no elements are taken (the result is an empty stream).

This is a short-circuiting stateful intermediate operation.

**Parameters:**

* `predicate` - a non-interfering, stateless predicate to apply to elements to determine the longest prefix of elements.

**Returns:** the new stream

</details>

<details>

<summary><code>&#x3C;A> A[] toArray(IntFunction&#x3C;A[]> generator)</code></summary>

Returns an array containing the elements of this stream, using the provided `generator` function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.

This is a terminal operation.

**Parameters:**

* `generator` - a function which produces a new array of the desired type and the provided length

**Returns:** an array containing the elements in this stream

</details>

<details>

<summary><code>Object[] toArray()</code></summary>

Returns an array containing the elements of this stream.

This is a terminal operation.

**Returns:** an array, whose `runtime component type` is `Object`, containing the elements of this stream

</details>

<details>

<summary><code>List&#x3C;T> toList()</code></summary>

Accumulates the elements of this stream into a `List`. The elements in the list will be in this stream's encounter order, if one exists. The returned List is unmodifiable; calls to any mutator method will always cause `UnsupportedOperationException` to be thrown. There are no guarantees on the implementation type or serializability of the returned List.

The returned instance may be value-based. Callers should make no assumptions about the identity of the returned instances. Identity-sensitive operations on these instances (reference equality (`==`), identity hash code, and synchronization) are unreliable and should be avoided.

This is a terminal operation.

**Returns:** a List containing the stream elements

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/types/stream.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
