ZetCode

Kotlin filter list

last modified June 2, 2022

Kotlin filter list tutorial shows how to filter list values in Kotlin language.

Kotlin has powerful methods for filtering list data.

The filter method returns a list containing only elements matching the given predicate. A predicate is a single-argument method that returns a boolean value.

Kotlin filter list example

In the first example, we use iteration to filter a list in Java.

simple.kt
package com.zetcode

fun main() {

    val vals = listOf(2, -1, 0, 5, 4, 9, -5, 11, 7)

    val res = vals.filter { e -> e > 0 }
    println(res)

    val res2 = vals.filter { e -> e < 0 }
    println(res2)
}

The example filters out positive and negative values from a list of integers.

Kotlin filterNot

The filterNot method returns values that do not match the given predicate.

filternot.kt
package com.zetcode

fun main() {

    val vals = listOf(2, -1, 0, 5, 4, 9, -5, 11, 7)

    val res = vals.filterNot { it > 0 }
    println(res)

    val res2 = vals.filterNot { it in listOf(0, 5, 4) }
    println(res2)
}

In the example, we filter out values that are not greater than zero and that do not belong to the given list of values.

Filtering strings

The next example filters strings.

filter_words.kt
package com.zetcode

fun main() {

    val words = listOf("war", "cup", "cool", "cloud", "water", 
        "ten", "pen")

    val res = words.filter { e -> e.length == 3 }
    println(res)

    val res2 = words.filter { e -> e.startsWith("w") }
    println(res2)
}

We have a list of words. We filter out all words whose size is equal to three and that begin with 'w' letter.

Multiple filter conditions

We can combine the filtering conditions with && and || operators.

multiple.kt
package com.zetcode

fun main() {

    val words = listOf("war", "cup", "cool", "cloud", 
        "water", "ten", "pen")

    val res = words.filter { e -> e.startsWith("w") || e.startsWith("c") }
    println(res)

    val res2 = words.filter { e -> e.startsWith("c") && e.contains("o") }
    println(res2)
}

The example filters out words that begin either with 'w' or 'c' and words that begin with 'c' and contain 'o'.

Kotlin filter objects

The following example fiters car objects.

filter_objects.kt
package com.zetcode

data class Car(val name: String, val price: Int)

fun main() {

    val vals = listOf(
        Car("Audi", 52642), Car("Mercedes", 57127),
        Car("Skoda", 9000), Car("Volvo", 29000),
        Car("Bentley", 350000), Car("Citroen", 21000),
        Car("Hummer", 41400), Car("Volkswagen", 21601)
    )

    val res = vals.filter { e -> e.name.startsWith("Vo") }
    println(res)

    val res2 = vals.filter { e -> e.price in 9001..49999 }
    println(res2)
}

We filter out cars whose names begin with "Vo" and cars whose price are in the given range.

In this tutorial, we have showed how to filter lists in Kotlin.

List all Kotlin tutorials.