Stay organized with collections
Save and categorize content based on your preferences.
Consumer
@FunctionalInterface interface Consumer<T : Any!>
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(java.lang.Object)
.
Summary
Public methods |
abstract Unit |
Performs this operation on the given argument.
|
open Consumer<T>! |
Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
|
Public methods
accept
abstract fun accept(t: T): Unit
Performs this operation on the given argument.
Parameters |
t |
T: the input argument |
andThen
open fun andThen(after: Consumer<in T>!): Consumer<T>!
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 |
Consumer<in T>!: the operation to perform after this operation |
Return |
Consumer<T>! |
a composed Consumer that performs in sequence this operation followed by the after operation |
Exceptions |
java.lang.NullPointerException |
if after is null |
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,["# Consumer\n\nAdded in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nConsumer\n========\n\n```\n@FunctionalInterface interface Consumer\u003cT : Any!\u003e\n```\n\n|----------------------------------|\n| [java.util.function.Consumer](#) |\n\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known Direct Subclasses [Stream.Builder](../stream/Stream.Builder.html#) |--------------------------------------------------|-----------------------------------| | [Stream.Builder](../stream/Stream.Builder.html#) | A mutable builder for a `Stream`. | |\n\nRepresents 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.\n\nThis is a [functional interface](/reference/kotlin/java/util/function/package-summary) whose functional method is [accept(java.lang.Object)](#accept(java.util.function.Consumer.T)).\n\nSummary\n-------\n\n| Public methods ||\n|---------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [accept](#accept(java.util.function.Consumer.T))`(`t:` `T`)` Performs this operation on the given argument. |\n| open [Consumer](#)\\\u003cT\\\u003e! | [andThen](#andThen(java.util.function.Consumer))`(`after:` `[Consumer](#)\u003cin` `T\u003e!`)` 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](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun accept(t: T): Unit\n```\n\nPerforms this operation on the given argument.\n\n| Parameters ||\n|-----|-----------------------|\n| `t` | T: the input argument |\n\n### andThen\n\nAdded in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun andThen(after: Consumer\u003cin T\u003e!): Consumer\u003cT\u003e!\n```\n\nReturns 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.\n\n| Parameters ||\n|---------|-----------------------------------------------------------------------|\n| `after` | [Consumer](#)\\\u003cin T\\\u003e!: the operation to perform after this operation |\n\n| Return ||\n|-------------------|--------------------------------------------------------------------------------------------------|\n| [Consumer](#)\u003cT\u003e! | a composed `Consumer` that performs in sequence this operation followed by the `after` operation |\n\n| Exceptions ||\n|----------------------------------|--------------------|\n| `java.lang.NullPointerException` | if `after` is null |"]]