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? ): CThis 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
list1containing the integers1, 2, 3. - We create an empty mutable list named
destinationto store non-null results. - We then apply the
mapIndexedNotNullTofunction tolist1, which takes a lambda with two parameters:indexandvalue. - 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
mapIndexedNotNullTofunction appends only the non-null results to thedestinationlist. - The resulting list, containing strings representing each even number along with its index, is stored in
result. - Finally, we print the value of
resultto 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
list1containing the characters'a', 'b', 'c'. - We create an empty mutable list named
destinationto store non-null results. - We then apply the
mapIndexedNotNullTofunction tolist1, which takes a lambda with two parameters:indexandvalue. - 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
mapIndexedNotNullTofunction appends only the non-null results to thedestinationlist. - The resulting list, containing strings representing the index and value of
'b', is stored inresult. - Finally, we print the value of
resultto 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
list1containing the strings'apple', 'banana', 'cherry'. - We create an empty mutable list named
destinationto store non-null results. - We then apply the
mapIndexedNotNullTofunction tolist1, which takes a lambda with two parameters:indexandvalue. - 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
mapIndexedNotNullTofunction appends only the non-null results to thedestinationlist. - 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
resultto 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.