Kotlin List mapIndexedNotNullTo()
Syntax & Examples
Syntax of List.mapIndexedNotNullTo()
The syntax of List.mapIndexedNotNullTo() extension function is:
fun <T, R : Any, C : MutableCollection<in R>> Iterable<T>.mapIndexedNotNullTo( destination: C, transform: (index: Int, T) -> R? ): C
This mapIndexedNotNullTo() extension function of List applies the given transform function to each element and its index in the original collection and appends only the non-null results to the given destination.
✐ Examples
1 Example
In this example,
- We create a list named
list1
containing the integers1, 2, 3
. - We create an empty mutable list named
destination
to store non-null results. - We then apply the
mapIndexedNotNullTo
function tolist1
, which takes a lambda with two parameters:index
andvalue
. - Within the lambda, we check if the value is even. If it is, we concatenate the index and value into a string format; otherwise, we return
null
. - The
mapIndexedNotNullTo
function appends only the non-null results to thedestination
list. - The resulting list, containing strings representing each even number along with its index, is stored in
result
. - Finally, we print the value of
result
to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf(1, 2, 3);
val destination = mutableListOf<String>();
val result = list1.mapIndexedNotNullTo(destination) { index, value -> if (value % 2 == 0) "\$index: \$value" else null }
print(result);
}
Output
[1: 2]
2 Example
In this example,
- We create a list named
list1
containing the characters'a', 'b', 'c'
. - We create an empty mutable list named
destination
to store non-null results. - We then apply the
mapIndexedNotNullTo
function tolist1
, which takes a lambda with two parameters:index
andvalue
. - Within the lambda, we check if the value is equal to
'b'
. If it is, we concatenate the index and value into a string format; otherwise, we returnnull
. - The
mapIndexedNotNullTo
function appends only the non-null results to thedestination
list. - The resulting list, containing strings representing the index and value of
'b'
, is stored inresult
. - Finally, we print the value of
result
to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("a", "b", "c");
val destination = mutableListOf<String>();
val result = list1.mapIndexedNotNullTo(destination) { index, value -> if (value == "b") "\$index: \$value" else null }
print(result);
}
Output
[1: b]
3 Example
In this example,
- We create a list named
list1
containing the strings'apple', 'banana', 'cherry'
. - We create an empty mutable list named
destination
to store non-null results. - We then apply the
mapIndexedNotNullTo
function tolist1
, which takes a lambda with two parameters:index
andvalue
. - Within the lambda, we check if the length of the string is greater than 5. If it is, we concatenate the index and value into a string format; otherwise, we return
null
. - The
mapIndexedNotNullTo
function appends only the non-null results to thedestination
list. - The resulting list, containing strings representing each string with a length greater than 5 along with its index, is stored in
result
. - Finally, we print the value of
result
to standard output.
Kotlin Program
fun main(args: Array<String>) {
val list1 = listOf("apple", "banana", "cherry");
val destination = mutableListOf<String>();
val result = list1.mapIndexedNotNullTo(destination) { index, value -> if (value.length > 5) "\$index: \$value" else null }
print(result);
}
Output
[0: banana, 1: cherry]
Summary
In this Kotlin tutorial, we learned about mapIndexedNotNullTo() extension function of List: the syntax and few working examples with output and detailed explanation for each example.