Kotlin List.filter() – Examples

Kotlin List.filter()

In Kotlin, the filter function is used to filter elements in a collection (such as a list) based on a given predicate. The filter function creates a new collection containing only the elements that satisfy the specified predicate.

Syntax

fun <T> Iterable<T>.filter(    predicate: (T) -> Boolean): List<T>

Returns a list containing only elements matching the given predicate.

Example 1: Filtering even numbers from a List

In this example,

  1. Take a list of numbers in the variable numbers.
  2. Call filter() function on the list numbers and specify the predicate to filter only even numbers.
  3. The filter() function returns a new list with the elements of numbers list that satisfy the given predicate of the item being even number.

Program

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    val evenNumbers = numbers.filter { it % 2 == 0 }

    println(evenNumbers)
}

Output

[2, 4, 6, 8, 10]

Example 2: Filtering strings in a List based on a condition

In this example,

  1. Take a list of strings in words.
  2. We have to filter the words whose length is greater than five.
  3. Call filter() function on the words list, and pass the predicate that the element length is greater than five.
  4. The filter() function returns a list containing words whose is greater than five.
  5. You may print the returned list to output.

Program

fun main() {
    val words = listOf("apple", "banana", "cherry", "date", "elderberry")

    val result = words.filter { it.length > 5 }

    println(result)
}

Output

[banana, cherry, elderberry]

Example 3: Filtering objects in a List based on a property

In this example,

  1. Take a list of objects in persons. A Person object has a name, and age as properties.
  2. We have to filter the persons list based on the age property of each object in the list.
  3. Call filter() function on the persons list, and pass the predicate that the age of the person is greater than 25.
  4. The filter() function returns a list containing Person objects whose age is greater than 25.
  5. You may print the returned list to output.

Program

fun main() {
    data class Person(val name: String, val age: Int)

    val persons = listOf(
        Person("Ram", 24),
        Person("Maddy", 30),
        Person("Krishna", 22),
        Person("Vina", 35)
    )

    val adults = persons.filter { it.age > 25 }

    println(adults)
}

Output

[Person(name=Maddy, age=30), Person(name=Vina, age=35)]

Example 4: Filtering null values from a List

In this example,

  1. Take a list of objects in mixedList. The list can contain objects of any type and also some null values.
  2. We have to filter non null values from the list.
  3. Call filter() function on the persons list, and pass the predicate that the item is not null.

Program

fun main() {
    val mixedList = listOf(1, null, "apple", 3, null, "orange", null, 7)

    val nonNullValues = mixedList.filter { it != null }

    println(nonNullValues)
}

Output

[1, apple, 3, orange, 7]

Otherwise, you can also use List.filterNotNull() instead of the filter and the predicate that the item is not null, as shown in the following.

Program

fun main() {
    val mixedList = listOf(1, null, "apple", 3, null, "orange", null, 7)

    val nonNullValues = mixedList.filterNotNull()

    println(nonNullValues)
}

Output

[1, apple, 3, orange, 7]