Kotlin Set mapNotNullTo()
Syntax & Examples
Set.mapNotNullTo() extension function
The mapNotNullTo() extension function in Kotlin applies the given transform function to each element in the original set, and appends only the non-null results to the given destination.
Syntax of Set.mapNotNullTo()
The syntax of Set.mapNotNullTo() extension function is:
fun <T, R : Any, C : MutableCollection<in R>> Set<T>.mapNotNullTo(destination: C, transform: (T) -> R?): C
This mapNotNullTo() extension function of Set applies the given transform function to each element in the original collection and appends only the non-null results to the given destination.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
destination | required | The collection to which the non-null results will be appended. |
transform | required | A function that takes an element and returns the transformed result, which may be null. |
Return Type
Set.mapNotNullTo() returns value of type C
.
✐ Examples
1 Transforming a set of integers by doubling each element, filtering out nulls, and appending to a list
Using mapNotNullTo() to transform a set of integers by doubling each element, filtering out null results, and appending them to a list.
For example,
- Create a set of integers.
- Create an empty mutable list to serve as the destination.
- Use mapNotNullTo() with a transform function that doubles each element and filters out nulls.
- Print the resulting list.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5)
val destination = mutableListOf<Int>()
numbers.mapNotNullTo(destination) { if (it % 2 == 0) it * 2 else null }
println(destination)
}
Output
[4, 8]
2 Transforming a set of strings by getting their lengths, filtering out nulls, and appending to a list
Using mapNotNullTo() to transform a set of strings by getting the length of each string, filtering out null results, and appending them to a list.
For example,
- Create a set of strings.
- Create an empty mutable list to serve as the destination.
- Use mapNotNullTo() with a transform function that returns the length of each string, and filters out nulls.
- Print the resulting list.
Kotlin Program
fun main() {
val strings = setOf("one", "two", "three")
val destination = mutableListOf<Int>()
strings.mapNotNullTo(destination) { if (it.length > 3) it.length else null }
println(destination)
}
Output
[5]
3 Transforming a set of custom objects by extracting a specific property, filtering out nulls, and appending to a list
Using mapNotNullTo() to transform a set of custom objects by extracting a specific property, filtering out null results, and appending them to a list.
For example,
- Create a data class.
- Create a set of custom objects.
- Create an empty mutable list to serve as the destination.
- Use mapNotNullTo() with a transform function that extracts a specific property from each object, and filters out nulls.
- Print the resulting list.
Kotlin Program
data class Person(val name: String, val age: Int)
fun main() {
val people = setOf(Person("Alice", 30), Person("Bob", 25), Person("Charlie", 35))
val destination = mutableListOf<String>()
people.mapNotNullTo(destination) { if (it.age > 30) it.name else null }
println(destination)
}
Output
[Charlie]
Summary
In this Kotlin tutorial, we learned about mapNotNullTo() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.