Stream.Builder
Stay organized with collections
Save and categorize content based on your preferences.
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.
Public Method Summary
abstract
void
|
accept(T t)
Adds an element to the stream being built.
|
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
public
abstract
void
accept
(T t)
Adds an element to the stream being built.
public
Builder<T>
add
(T t)
Adds an element to the stream being built.
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-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 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eStream.Builder\u003c/code\u003e allows the creation of a \u003ccode\u003eStream\u003c/code\u003e by adding elements individually, avoiding the overhead of using temporary buffers.\u003c/p\u003e\n"],["\u003cp\u003eThe builder has two phases: a building phase where elements can be added and a built phase (triggered by \u003ccode\u003ebuild()\u003c/code\u003e) where the stream is created and no more elements can be added.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eaccept(T t)\u003c/code\u003e and \u003ccode\u003eadd(T t)\u003c/code\u003e methods are used to add elements to the stream during the building phase.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ebuild()\u003c/code\u003e method creates the \u003ccode\u003eStream\u003c/code\u003e from the added elements and transitions the builder to the built state, preventing further modifications.\u003c/p\u003e\n"]]],[],null,["public static interface **Stream.Builder** implements [Consumer](../../../../reference/java/util/function/Consumer.html)\\\u003cT\\\u003e \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.html#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\nSee Also\n\n- [Stream.builder()](../../../../reference/java/util/stream/Stream.html#builder()) \n\nPublic Method Summary\n\n|----------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [accept](../../../../reference/java/util/stream/Stream.Builder.html#accept(T))(T t) Adds an element to the stream being built. |\n| [Builder](../../../../reference/java/util/stream/Stream.Builder.html)\\\u003cT\\\u003e | [add](../../../../reference/java/util/stream/Stream.Builder.html#add(T))(T t) Adds an element to the stream being built. |\n| abstract [Stream](../../../../reference/java/util/stream/Stream.html)\\\u003cT\\\u003e | [build](../../../../reference/java/util/stream/Stream.Builder.html#build())() Builds the stream, transitioning this builder to the built state. |\n\nInherited Method Summary \nFrom interface [java.util.function.Consumer](../../../../reference/java/util/function/Consumer.html) \n\n|-------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [accept](../../../../reference/java/util/function/Consumer.html#accept(T))(T t) Performs this operation on the given argument. |\n| [Consumer](../../../../reference/java/util/function/Consumer.html)\\\u003cT\\\u003e | [andThen](../../../../reference/java/util/function/Consumer.html#andThen(java.util.function.Consumer\u003c?%20super%20T\u003e))([Consumer](../../../../reference/java/util/function/Consumer.html)\\\u003c? super T\\\u003e after) Returns a composed `Consumer` that performs, in sequence, this operation followed by the `after` operation. |\n\nPublic Methods \n\npublic abstract void\n**accept**\n(T t) \nAdds an element to the stream being built. \n\nParameters\n\n| t | the input argument |\n|---|--------------------|\n\nThrows\n\n| [IllegalStateException](../../../../reference/java/lang/IllegalStateException.html) | if the builder has already transitioned to the built state |\n|-------------------------------------------------------------------------------------|------------------------------------------------------------|\n\npublic [Builder](../../../../reference/java/util/stream/Stream.Builder.html)\\\u003cT\\\u003e\n**add**\n(T t) \nAdds an element to the stream being built. \n\nParameters\n\n| t | the element to add |\n|---|--------------------|\n\nReturns\n\n- `this` builder \n\nThrows\n\n| [IllegalStateException](../../../../reference/java/lang/IllegalStateException.html) | if the builder has already transitioned to the built state |\n|-------------------------------------------------------------------------------------|------------------------------------------------------------|\n\npublic abstract [Stream](../../../../reference/java/util/stream/Stream.html)\\\u003cT\\\u003e\n**build**\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\nReturns\n\n- the built stream \n\nThrows\n\n| [IllegalStateException](../../../../reference/java/lang/IllegalStateException.html) | if the builder has already transitioned to the built state |\n|-------------------------------------------------------------------------------------|------------------------------------------------------------|"]]