0Pricing
Kotlin Academy · Lesson

Filtering and Mapping

Transform collections.

Filtering and Mapping is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Transforming Collections

Kotlin collections come with a rich set of functional operations. Two of the most common are filter (select elements) and map (transform elements). They return new collections and never modify the original.

The filter Function

filter keeps only the elements that satisfy a predicate, returning a new list.

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

filterNot and filterNotNull

filterNot keeps elements that do NOT match. filterNotNull removes null elements and returns a non-null list.

fun main() {
    val nums = listOf(1, 2, 3, 4)
    println(nums.filterNot { it > 2 })
    val withNulls = listOf(1, null, 3, null)
    println(withNulls.filterNotNull())
}

filterIndexed

filterIndexed gives you both the index and the value in the predicate.

fun main() {
    val letters = listOf("a", "b", "c", "d")
    val result = letters.filterIndexed { index, _ -> index % 2 == 0 }
    println(result)
}

The map Function

map applies a transformation to every element and returns a new list of the results. The result type can differ from the input type.

fun main() {
    val numbers = listOf(1, 2, 3)
    val squares = numbers.map { it * it }
    println(squares)
}

Mapping to Another Type

map can change the element type, for example turning numbers into strings.

fun main() {
    val numbers = listOf(1, 2, 3)
    val labels = numbers.map { "Item $it" }
    println(labels)
}

mapIndexed

mapIndexed provides the index alongside each element so you can use position in the transform.

fun main() {
    val fruits = listOf("apple", "banana", "cherry")
    val numbered = fruits.mapIndexed { i, fruit -> "${i + 1}. $fruit" }
    numbered.forEach { println(it) }
}

mapNotNull

mapNotNull transforms each element and drops any results that are null. It is a clean way to combine mapping and filtering.

fun main() {
    val strings = listOf("1", "two", "3", "four")
    val numbers = strings.mapNotNull { it.toIntOrNull() }
    println(numbers)
}

Chaining filter and map

These operations compose naturally. A common pattern is to filter first, then map the survivors.

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6)
    val result = numbers
        .filter { it % 2 == 0 }
        .map { it * 10 }
    println(result)
}

Operations Are Non-Destructive

Both filter and map return brand new lists. The original collection is unchanged, which makes your code safer and easier to reason about.

fun main() {
    val original = listOf(1, 2, 3)
    val doubled = original.map { it * 2 }
    println("Original: $original")
    println("Doubled: $doubled")
}

Filtering and Mapping in Practice

Together, filter and map let you express data pipelines declaratively: describe WHAT you want, not HOW to loop. This is clearer than manual for loops with index tracking.

Quick Check

What is the difference between filter and map?

Recap

You learned to select elements with filter (and its variants filterNot, filterIndexed, filterNotNull) and transform them with map (and mapIndexed, mapNotNull). They chain cleanly and never mutate the source. Next you will organize data with grouping and partitioning.

Frequently asked questions

Is the “Filtering and Mapping” lesson free?

Yes — the full text of “Filtering and Mapping” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Filtering and Mapping”?

Transform collections. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Filtering and Mapping” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Filtering and Mapping
  2. Grouping and Partitioning
  3. Aggregation
  4. Association and Flattening
← Back to Kotlin Academy