Consumer
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
Represents an operation that accepts a single input argument and returns no
result. Unlike most other functional interfaces, Consumer
is expected
to operate via side-effects.
This is a functional interface
whose functional method is accept(Object)
.
Public Method Summary
abstract
void
|
accept(T t)
Performs this operation on the given argument.
|
Consumer<T>
|
andThen( Consumer<? super T> after)
Returns a composed Consumer that performs, in sequence, this
operation followed by the after operation.
|
Public Methods
public
abstract
void
accept
(T t)
Performs this operation on the given argument.
public
Consumer<T>
andThen
(Consumer<? super T> after)
Returns a composed Consumer
that performs, in sequence, this
operation followed by the after
operation. If performing either
operation throws an exception, it is relayed to the caller of the
composed operation. If performing this operation throws an exception,
the after
operation will not be performed.
Parameters
after |
the operation to perform after this operation |
Returns
- a composed
Consumer
that performs in sequence this
operation followed by the after
operation
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\u003eConsumer\u003c/code\u003e is a functional interface representing an operation that takes an input and produces no result, primarily working through side-effects.\u003c/p\u003e\n"],["\u003cp\u003eIt includes the \u003ccode\u003eaccept\u003c/code\u003e method to perform the operation on the given input and the \u003ccode\u003eandThen\u003c/code\u003e method to chain multiple \u003ccode\u003eConsumer\u003c/code\u003e instances for sequential execution.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eConsumer\u003c/code\u003e is commonly used in scenarios where actions need to be performed on data without necessarily returning a value, such as logging or modifying data structures.\u003c/p\u003e\n"]]],["The `Consumer` interface represents an operation with a single input argument and no return value, typically using side-effects. It features the `accept(T t)` method, which executes the operation on the provided argument. Another key method is `andThen(Consumer\u003c? super T\u003e after)`, which allows sequencing operations by executing the `after` operation subsequent to the primary operation. `andThen` will relay exceptions to the caller or if the first operation fails it will not perform the `after` operation.\n"],null,["# Consumer\n\npublic interface **Consumer** \n\n|---|---|---|\n| Known Indirect Subclasses [Stream.Builder](../../../../reference/java/util/stream/Stream.Builder.html)\\\u003cT\\\u003e |-----------------------------------------------------------------------------------|-----------------------------------| | [Stream.Builder](../../../../reference/java/util/stream/Stream.Builder.html)\\\u003cT\\\u003e | A mutable builder for a `Stream`. | |||\n\nRepresents an operation that accepts a single input argument and returns no\nresult. Unlike most other functional interfaces, `Consumer` is expected\nto operate via side-effects.\n\nThis is a [functional interface](/j2objc/javadoc/jre/reference/java/util/function/package-summary)\nwhose functional method is [accept(Object)](../../../../reference/java/util/function/Consumer.html#accept(T)). \n\n### Public Method Summary\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--------------\n\n#### public abstract void\n**accept**\n(T t)\n\nPerforms this operation on the given argument. \n\n##### Parameters\n\n| t | the input argument |\n|---|--------------------|\n\n#### public [Consumer](../../../../reference/java/util/function/Consumer.html)\\\u003cT\\\u003e\n**andThen**\n([Consumer](../../../../reference/java/util/function/Consumer.html)\\\u003c? super T\\\u003e after)\n\nReturns a composed `Consumer` that performs, in sequence, this\noperation followed by the `after` operation. If performing either\noperation throws an exception, it is relayed to the caller of the\ncomposed operation. If performing this operation throws an exception,\nthe `after` operation will not be performed. \n\n##### Parameters\n\n| after | the operation to perform after this operation |\n|-------|-----------------------------------------------|\n\n##### Returns\n\n- a composed `Consumer` that performs in sequence this operation followed by the `after` operation \n\n##### Throws\n\n| [NullPointerException](../../../../reference/java/lang/NullPointerException.html) | if `after` is null |\n|-----------------------------------------------------------------------------------|--------------------|"]]