Kotlin Set filterNotTo()
Syntax & Examples
Set.filterNotTo() extension function
The filterNotTo() extension function in Kotlin filters elements in a set, appending all elements that do not match the given predicate to the specified destination collection.
Syntax of Set.filterNotTo()
The syntax of Set.filterNotTo() extension function is:
fun <T, C : MutableCollection<in T>> Set<T>.filterNotTo(destination: C, predicate: (T) -> Boolean): C
This filterNotTo() extension function of Set appends all elements not matching the given predicate to the given destination.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
destination | required | The collection to which the elements not matching the predicate will be appended. |
predicate | required | A function that takes an element and returns true if the element should be excluded from the result. |
Return Type
Set.filterNotTo() returns value of type C
.
✐ Examples
1 Appending non-even numbers to a list
Using filterNotTo() to filter elements in a set, appending non-even numbers to a list.
For example,
- Create a set of integers.
- Create an empty list to hold the non-even elements.
- Define a predicate function that returns true for even numbers.
- Use filterNotTo() to append non-even numbers from the set to the list.
- Print the resulting list.
Kotlin Program
fun main() {
val numbers = setOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val nonEvenList = mutableListOf<Int>()
numbers.filterNotTo(nonEvenList) { it % 2 == 0 }
println(nonEvenList)
}
Output
[1, 3, 5, 7, 9]
2 Appending non-null values to a set
Using filterNotTo() to filter elements in a set, appending non-null values to another set.
For example,
- Create a set containing integers and null values.
- Create an empty set to hold the non-null elements.
- Define a predicate function that returns true for null values.
- Use filterNotTo() to append non-null values from the set to the new set.
- Print the resulting set.
Kotlin Program
fun main() {
val mixedSet: Set<Int?> = setOf(1, 2, null, 4, null, 6)
val nonNullSet = mutableSetOf<Int?>()
mixedSet.filterNotTo(nonNullSet) { it == null }
println(nonNullSet)
}
Output
[1, 2, 4, 6]
3 Appending strings with length greater than 2 to a list
Using filterNotTo() to filter elements in a set, appending strings with length greater than 2 to a list.
For example,
- Create a set of strings.
- Create an empty list to hold the strings with length greater than 2.
- Define a predicate function that returns true for strings with length less than or equal to 2.
- Use filterNotTo() to append strings with length greater than 2 from the set to the list.
- Print the resulting list.
Kotlin Program
fun main() {
val strings = setOf("a", "ab", "abc", "abcd")
val longStrings = mutableListOf<String>()
strings.filterNotTo(longStrings) { it.length <= 2 }
println(longStrings)
}
Output
["abc", "abcd"]
Summary
In this Kotlin tutorial, we learned about filterNotTo() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.