Kotlin .filterNot()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 28, 2023
Contribute to Docs

The .filterNot() method will filter elements from a Kotlin sequence based on whether or not the same element is present in the given predicate.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours

Syntax

String.filterNot(predicate)

-String: The collection of elements to be filtered. -predicate: The collection of elements to be filtered out.

Example

The example demonstrates the use of .filterNot() to remove any pairs in the map with a value less than 2, and then to filter out any pairs with the key c.

fun main() {
val originalMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val filteredMap = originalMap.filterNot(it.value < 2)
println(filteredMap)
val filteredMap2 = originalMap.filterNot(it.key == "c")
println(filteredMap2)
}

The output of this code will be:

{b=2, c=3}
{a=1, b=2}

All contributors

Contribute to Docs

Learn Kotlin on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours