Stay organized with collections
Save and categorize content based on your preferences.
Stream.Builder
public
static
interface
Stream.Builder
implements
Consumer<T>
java.util.stream.Stream.Builder<T>
|
A mutable builder for a Stream
. This allows the creation of a
Stream
by generating elements individually and adding them to the
Builder
(without the copying overhead that comes from using
an ArrayList
as a temporary buffer.)
A stream builder has a lifecycle, which starts in a building
phase, during which elements can be added, and then transitions to a built
phase, after which elements may not be added. The built phase begins
when the build()
method is called, which creates an ordered
Stream
whose elements are the elements that were added to the stream
builder, in the order they were added.
Summary
Public methods |
abstract
void
|
accept(T t)
Adds an element to the stream being built.
|
default
Builder<T>
|
add(T t)
Adds an element to the stream being built.
|
abstract
Stream<T>
|
build()
Builds the stream, transitioning this builder to the built state.
|
Public methods
accept
public abstract void accept (T t)
Adds an element to the stream being built.
Parameters |
t |
T : the input argument |
add
public Builder<T> add (T t)
Adds an element to the stream being built.
Implementation Requirements:
- The default implementation behaves as if:
accept(t)
return this;
Parameters |
t |
T : the element to add |
build
public abstract Stream<T> build ()
Builds the stream, transitioning this builder to the built state.
An IllegalStateException
is thrown if there are further attempts
to operate on the builder after it has entered the built state.
Returns |
Stream<T> |
the built stream |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Stream.Builder\n\nAdded in [API level 24](/guide/topics/manifest/uses-sdk-element#ApiLevels) \nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nStream.Builder\n==============\n\n\n`\npublic\nstatic\n\n\ninterface\nStream.Builder\n`\n\n\n`\n\n\nimplements\n\n`[Consumer](/reference/java/util/function/Consumer)`\u003cT\u003e\n\n\n`\n\n|--------------------------------------|\n| java.util.stream.Stream.Builder\\\u003cT\\\u003e |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nA mutable builder for a `Stream`. This allows the creation of a\n`Stream` by generating elements individually and adding them to the\n`Builder` (without the copying overhead that comes from using\nan `ArrayList` as a temporary buffer.)\n\nA stream builder has a lifecycle, which starts in a building\nphase, during which elements can be added, and then transitions to a built\nphase, after which elements may not be added. The built phase begins\nwhen the [build()](/reference/java/util/stream/Stream.Builder#build()) method is called, which creates an ordered\n`Stream` whose elements are the elements that were added to the stream\nbuilder, in the order they were added. \n**See also:**\n\n- [Stream.builder()](/reference/java/util/stream/Stream#builder())\n\nSummary\n-------\n\n| ### Public methods ||\n|-----------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract void` | ` `[accept](/reference/java/util/stream/Stream.Builder#accept(T))`(T t) ` Adds an element to the stream being built. |\n| ` default `[Builder](/reference/java/util/stream/Stream.Builder)`\u003cT\u003e` | ` `[add](/reference/java/util/stream/Stream.Builder#add(T))`(T t) ` Adds an element to the stream being built. |\n| ` abstract `[Stream](/reference/java/util/stream/Stream)`\u003cT\u003e` | ` `[build](/reference/java/util/stream/Stream.Builder#build())`() ` Builds the stream, transitioning this builder to the built state. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` `[java.util.function.Consumer](/reference/java/util/function/Consumer)` ` |--------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract void` | ` `[accept](/reference/java/util/function/Consumer#accept(T))`(T t) ` Performs this operation on the given argument. | | ` default `[Consumer](/reference/java/util/function/Consumer)`\u003cT\u003e` | ` `[andThen](/reference/java/util/function/Consumer#andThen(java.util.function.Consumer\u003c?%20super%20T\u003e))`(`[Consumer](/reference/java/util/function/Consumer)`\u003c? super T\u003e after) ` Returns a composed `Consumer` that performs, in sequence, this operation followed by the `after` operation. | ||\n\nPublic methods\n--------------\n\n### accept\n\nAdded in [API level 24](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract void accept (T t)\n```\n\nAdds an element to the stream being built.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|-----|--------------------------------|\n| `t` | `T`: the input argument \u003cbr /\u003e |\n\n| Throws ||\n|---------------------------------------------------------------------|------------------------------------------------------------|\n| [IllegalStateException](/reference/java/lang/IllegalStateException) | if the builder has already transitioned to the built state |\n\n### add\n\nAdded in [API level 24](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic Builder\u003cT\u003e add (T t)\n```\n\nAdds an element to the stream being built.\n\n\u003cbr /\u003e\n\n##### Implementation Requirements:\n\n- The default implementation behaves as if: \n\n accept(t)\n return this;\n \n| Parameters ||\n|-----|--------------------------------|\n| `t` | `T`: the element to add \u003cbr /\u003e |\n\n| Returns ||\n|------------------------------------------------------------|-----------------------|\n| [Builder](/reference/java/util/stream/Stream.Builder)`\u003cT\u003e` | `this` builder \u003cbr /\u003e |\n\n| Throws ||\n|---------------------------------------------------------------------|------------------------------------------------------------|\n| [IllegalStateException](/reference/java/lang/IllegalStateException) | if the builder has already transitioned to the built state |\n\n### build\n\nAdded in [API level 24](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract Stream\u003cT\u003e build ()\n```\n\nBuilds the stream, transitioning this builder to the built state.\nAn `IllegalStateException` is thrown if there are further attempts\nto operate on the builder after it has entered the built state.\n\n\u003cbr /\u003e\n\n| Returns ||\n|---------------------------------------------------|-------------------------|\n| [Stream](/reference/java/util/stream/Stream)`\u003cT\u003e` | the built stream \u003cbr /\u003e |\n\n| Throws ||\n|---------------------------------------------------------------------|------------------------------------------------------------|\n| [IllegalStateException](/reference/java/lang/IllegalStateException) | if the builder has already transitioned to the built state |"]]