Skip to content
Home » Kotlin Map Filter

Kotlin Map Filter

Kotlin Map Filter

1. Overview

In this article, we will discuss the Kotlin Map filter with examples.

2. Kotlin Map Filter

Usually, you had to iterate your map and filter the elements based on certain conditions. Thus, Filtering the map is one of the most basic and familiar tasks.

Kotlin provides the following functions to filter your map:

  • filter
  • filterNot

Both functions take a predicate as the parameter. A predicate is a lambda that takes each map element and checks whether it satisfies the required condition. It returns a true or false value.

Other functions such as filterIndexed, partition, filterIsInstance, filterNull function do not work with Map and applicable only for List.

The filter function returns the map elements that match a condition, whereas the filterNot function returns the map elements that don’t match the condition. Now let’s see each of these functions.

2. Kotlin Filter function

The filter function is a basic filtering function. When you call the filter function with a predicate, it returns the map elements that match it. Therefore, the output of the function is also a Map.

For example, the below code has a Student map that contains student id as key and student name as value. Assume you need to filter the students whose name starts with the letter “A”.

Here, you can use the filter function and pass the predicate to it. The predicate will take the map element as input and use the startsWith function to check whether the student name starts with “A”.

fun main() {
   val studentsMap = mapOf(101 to "Asha", 102 to "John", 103 to "Afra", 104 to "Kyle")
   val filteredMap = studentsMap.filter { (_, value) -> value.startsWith("A")}
   println(filteredMap) /* prints {101=Asha, 103=Afra} */
}

Note that the (_, value) is a destructuring declaration.

3. Kotlin filterNot function

The filterNot function returns the map elements that do not match the provided condition.

For example, the below code uses the filterNot function to filter the students whose name doesn’t start with K.

fun main() {
   val studentsMap = mapOf(1 to "Asha", 2 to "John", 3 to "Afra", 4 to "Kyle")
   val filteredMap = studentsMap.filterNot { (_, value): Map.Entry<Int, String> -> value.startsWith("K")}
   println(filteredMap) /* prints {1=Asha, 2=John, 3=Afra} */
}

4. Kotlin filter map by key

You can use filter function to filter a map by using its key.

Assume you want to filter students whose student id is less than 103. Here, you can use the filter function.

fun main() {
   val studentsMap = mapOf(101 to "Asha", 102 to "John", 103 to "Afra", 104 to "Kyle")
   val filteredMap = studentsMap.filter { (key, _) -> (key < 103)}
   println(filteredMap) /* prints {101=Asha, 102=John} */
}

4. Kotlin filter map by value

You can use filter function to filter a map by using its value.

Assume you want to filter students whose name ends with “a”. Here, you can use the filter function.

fun main() {
   val studentsMap = mapOf(101 to "Asha", 102 to "John", 103 to "Afra", 104 to "Kyle")
   val filteredMap = studentsMap.filter { (_, value) -> values.endsWith("a)}
   println(filteredMap) /* prints {101=Asha, 103=Afra} */
}

6. Conclusion

To sum up, this article explained the standard Kotlin functions available to filter the Map based on certain conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *