Stay organized with collections
Save and categorize content based on your preferences.
Function
@FunctionalInterface interface Function<T : Any!, R : Any!>
Known Direct Subclasses
UnaryOperator |
Represents an operation on a single operand that produces a result of the same type as its operand.
|
|
Represents a function that accepts one argument and produces a result.
This is a functional interface whose functional method is apply(java.lang.Object)
.
Summary
Public methods |
open Function<T, V>! |
Returns a composed function that first applies this function to its input, and then applies the after function to the result.
|
abstract R |
Applies this function to the given argument.
|
open Function<V, R>! |
Returns a composed function that first applies the before function to its input, and then applies this function to the result.
|
open static Function<T, T>! |
Returns a function that always returns its input argument.
|
Public methods
andThen
open fun <V : Any!> andThen(after: Function<in R, out V>!): Function<T, V>!
Returns a composed function that first applies this function to its input, and then applies the after
function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
Parameters |
<V> |
the type of output of the after function, and of the composed function |
after |
Function<in R, out V>!: the function to apply after this function is applied |
Return |
Function<T, V>! |
a composed function that first applies this function and then applies the after function |
Exceptions |
java.lang.NullPointerException |
if after is null |
apply
abstract fun apply(t: T): R
Applies this function to the given argument.
Parameters |
t |
T: the function argument |
Return |
R |
the function result |
compose
open fun <V : Any!> compose(before: Function<in V, out T>!): Function<V, R>!
Returns a composed function that first applies the before
function to its input, and then applies this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.
Parameters |
<V> |
the type of input to the before function, and to the composed function |
before |
Function<in V, out T>!: the function to apply before this function is applied |
Return |
Function<V, R>! |
a composed function that first applies the before function and then applies this function |
Exceptions |
java.lang.NullPointerException |
if before is null |
identity
open static fun <T : Any!> identity(): Function<T, T>!
Returns a function that always returns its input argument.
Parameters |
<T> |
the type of the input and output objects to the function |
Return |
Function<T, T>! |
a function that always returns its input argument |
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.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# Function\n\nAdded in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nFunction\n========\n\n```\n@FunctionalInterface interface Function\u003cT : Any!, R : Any!\u003e\n```\n\n|----------------------------------|\n| [java.util.function.Function](#) |\n\n|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Known Direct Subclasses [UnaryOperator](/reference/kotlin/java/util/function/UnaryOperator) |---------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------| | [UnaryOperator](/reference/kotlin/java/util/function/UnaryOperator) | Represents an operation on a single operand that produces a result of the same type as its operand. | |\n\nRepresents a function that accepts one argument and produces a result.\n\nThis is a [functional interface](/reference/kotlin/java/util/function/package-summary) whose functional method is [apply(java.lang.Object)](#apply(java.util.function.Function.T)).\n\nSummary\n-------\n\n| Public methods ||\n|------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| open [Function](#)\\\u003cT, V\\\u003e! | [andThen](#andThen(java.util.function.Function))`(`after:` `[Function](#)\u003cin` `R,` `out` `V\u003e!`)` Returns a composed function that first applies this function to its input, and then applies the `after` function to the result. |\n| abstract R | [apply](#apply(java.util.function.Function.T))`(`t:` `T`)` Applies this function to the given argument. |\n| open [Function](#)\\\u003cV, R\\\u003e! | [compose](#compose(java.util.function.Function))`(`before:` `[Function](#)\u003cin` `V,` `out` `T\u003e!`)` Returns a composed function that first applies the `before` function to its input, and then applies this function to the result. |\n| open static [Function](#)\\\u003cT, T\\\u003e! | [identity](#identity())`()` Returns a function that always returns its input argument. |\n\nPublic methods\n--------------\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 \u003cV : Any!\u003e andThen(after: Function\u003cin R, out V\u003e!): Function\u003cT, V\u003e!\n```\n\nReturns a composed function that first applies this function to its input, and then applies the `after` function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.\n\n| Parameters ||\n|---------|-------------------------------------------------------------------------------------|\n| `\u003cV\u003e` | the type of output of the `after` function, and of the composed function |\n| `after` | [Function](#)\\\u003cin R, out V\\\u003e!: the function to apply after this function is applied |\n\n| Return ||\n|------------------------|--------------------------------------------------------------------------------------------|\n| [Function](#)\u003cT,` `V\u003e! | a composed function that first applies this function and then applies the `after` function |\n\n| Exceptions ||\n|----------------------------------|------------------|\n| `java.lang.NullPointerException` | if after is null |\n\n**See Also**\n\n- [#compose(Function)](#compose(java.util.function.Function)) \n\n### apply\n\nAdded in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun apply(t: T): R\n```\n\nApplies this function to the given argument.\n\n| Parameters ||\n|-----|--------------------------|\n| `t` | T: the function argument |\n\n| Return ||\n|---|---------------------|\n| R | the function result |\n\n### compose\n\nAdded in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun \u003cV : Any!\u003e compose(before: Function\u003cin V, out T\u003e!): Function\u003cV, R\u003e!\n```\n\nReturns a composed function that first applies the `before` function to its input, and then applies this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.\n\n| Parameters ||\n|----------|--------------------------------------------------------------------------------------|\n| `\u003cV\u003e` | the type of input to the `before` function, and to the composed function |\n| `before` | [Function](#)\\\u003cin V, out T\\\u003e!: the function to apply before this function is applied |\n\n| Return ||\n|------------------------|---------------------------------------------------------------------------------------------|\n| [Function](#)\u003cV,` `R\u003e! | a composed function that first applies the `before` function and then applies this function |\n\n| Exceptions ||\n|----------------------------------|-------------------|\n| `java.lang.NullPointerException` | if before is null |\n\n**See Also**\n\n- [#andThen(Function)](#andThen(java.util.function.Function)) \n\n### identity\n\nAdded in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen static fun \u003cT : Any!\u003e identity(): Function\u003cT, T\u003e!\n```\n\nReturns a function that always returns its input argument.\n\n| Parameters ||\n|-------|----------------------------------------------------------|\n| `\u003cT\u003e` | the type of the input and output objects to the function |\n\n| Return ||\n|------------------------|---------------------------------------------------|\n| [Function](#)\u003cT,` `T\u003e! | a function that always returns its input argument |"]]