Kotlin Tutorials

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

ParameterOptional/RequiredDescription
destinationrequiredThe collection to which the elements not matching the predicate will be appended.
predicaterequiredA 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,

  1. Create a set of integers.
  2. Create an empty list to hold the non-even elements.
  3. Define a predicate function that returns true for even numbers.
  4. Use filterNotTo() to append non-even numbers from the set to the list.
  5. 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,

  1. Create a set containing integers and null values.
  2. Create an empty set to hold the non-null elements.
  3. Define a predicate function that returns true for null values.
  4. Use filterNotTo() to append non-null values from the set to the new set.
  5. 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,

  1. Create a set of strings.
  2. Create an empty list to hold the strings with length greater than 2.
  3. Define a predicate function that returns true for strings with length less than or equal to 2.
  4. Use filterNotTo() to append strings with length greater than 2 from the set to the list.
  5. 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.