0Pricing
Kotlin Academy · Lesson

filter, filterNot, and partition

Split and select elements using predicate-based functions.

Selection Operations

filter keeps elements matching a predicate. filterNot keeps those that do NOT match. partition splits the collection into two lists at once.

Basic filter

filter { ... } returns a new list containing only elements where the predicate is true.

fun main() {
    val nums = listOf(1, 2, 3, 4, 5, 6)
    val evens = nums.filter { it % 2 == 0 }
    println(evens) // [2, 4, 6]
}

All lessons in this course

  1. map and flatMap: Transforming Every Element
  2. filter, filterNot, and partition
  3. fold, reduce, and runningFold
  4. Chaining Pipelines and Avoiding Intermediate Lists with Sequence
← Back to Kotlin Academy