Skip to main content
freelanceshack.com

Back to all posts

How to Use the "Filter" And "Map" Functions on Collections In Kotlin?

Published on
6 min read
How to Use the "Filter" And "Map" Functions on Collections In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
2 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

  • HIGH-QUALITY, DURABLE MATERIALS FOR LONG-LASTING PERFORMANCE.
  • USER-FRIENDLY DESIGN FOR SEAMLESS OPERATION AND CONVENIENCE.
  • AFFORDABLE PRICING WITHOUT COMPROMISING ON SUPERIOR FEATURES.
BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
3 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
4 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
7 Atomic Kotlin

Atomic Kotlin

BUY & SAVE
$44.91 $49.00
Save 8%
Atomic Kotlin
8 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
+
ONE MORE?

In Kotlin, the "filter" and "map" functions are useful for manipulating collections such as lists, arrays, or sequences.

The "filter" function allows you to selectively retain elements from a collection based on a given condition. It takes a predicate that determines whether an element should be included in the resulting collection. The filtered elements are returned as a new collection.

For example:

val numbers = listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 == 0 } // Result: [2, 4]

In this example, the "filter" function is used to keep only the even numbers from the list of "numbers". The condition inside the lambda expression checks if each element is divisible by 2.

The "map" function, on the other hand, transforms each element of a collection based on a given transformation rule. It applies the transformation to each element and returns a new collection with the transformed values.

For example:

val numbers = listOf(1, 2, 3, 4, 5) val squaredNumbers = numbers.map { it * it } // Result: [1, 4, 9, 16, 25]

In this case, the "map" function is used to square each element of the "numbers" list. The lambda expression specifies the transformation rule.

Both "filter" and "map" functions are useful for functional programming and provide a concise way to manipulate collections in Kotlin. They help in performing common operations on collections without the need for manual iteration.

How to use the mapNotNull function on collections in Kotlin?

The mapNotNull function is used to transform a collection by applying a transformation function to each element and then filtering out the resulting null values.

Here's how you can use mapNotNull function on collections in Kotlin:

  1. Start by creating a collection (e.g., a list):

val numbers = listOf(1, 2, 3, 4, 5)

  1. Use the mapNotNull function to transform the collection, providing a lambda function as the transformation function that returns the transformed value (or null if necessary):

val transformedList = numbers.mapNotNull { number -> if (number % 2 == 0) { number * 2 // Return the transformed value } else { null // Filter out odd numbers by returning null } }

In this example, the lambda function checks if an element is even (i.e., divisible by 2), and if so, it returns the element multiplied by 2. If an element is odd, it returns null.

  1. The resulting transformedList will only contain the non-null transformed elements:

println(transformedList) // Output: [4, 8]

In this case, only the even numbers were transformed and included in the result.

Note that mapNotNull function returns a new collection containing the transformed non-null elements, without modifying the original collection.

How to apply multiple map functions in Kotlin?

In Kotlin, you can apply multiple map functions by chaining them together using the map function. Here's an example:

val numbers = listOf(1, 2, 3, 4, 5)

val squaredEvenNumbers = numbers .filter { it % 2 == 0 } // Filter even numbers .map { it * it } // Square each number

println(squaredEvenNumbers) // Output: [4, 16]

In this example, we start with a list of numbers and apply two map functions. First, we filter out the even numbers using the filter function, and then we square each number using the map function. The result is a list of squared even numbers.

You can chain as many map functions as you need to apply multiple transformations to your data.

How to use the filterNotNull function on collections in Kotlin?

In Kotlin, the filterNotNull function can be used to filter out all the null values from a collection. Here's how you can use it:

val list: List<String?> = listOf("hello", null, "world", null, "kotlin", null)

val filteredList: List = list.filterNotNull()

println(filteredList) // [hello, world, kotlin]

In the code above, we have a list of nullable strings (String?) that contains some null values. We use the filterNotNull function to get a new list (filteredList) that contains only the non-null values from the original list.

Note that the resulting list filteredList has type List<String> because the filterNotNull function removes all null values, effectively converting the nullable type to a non-null type.

You can use the filterNotNull function directly on other types of collections, such as arrays and sets, in a similar way.

How to filter a list based on a given condition in Kotlin?

In Kotlin, you can use the filter() function to filter a list based on a given condition. Here's an example:

val numbers = listOf(1, 2, 3, 4, 5)

val evenNumbers = numbers.filter { it % 2 == 0 } println(evenNumbers) // Prints [2, 4]

In this example, the filter() function takes a lambda expression as a parameter, which specifies the condition for filtering. The lambda expression { it % 2 == 0 } checks if an element is divisible by 2 (i.e., an even number), and returns true if the condition is met.

The filter() function returns a new list that contains only the elements that satisfy the given condition. In this case, it creates a list evenNumbers that contains only the even numbers from the original numbers list.

You can replace the condition { it % 2 == 0 } with any other condition that suits your filtering needs.

What is the difference between filter and filterNot functions in Kotlin?

The filter and filterNot functions in Kotlin are used to filter elements from a collection based on a specified condition. The key difference between them lies in the filtering logic:

  1. filter function: It retains the elements that satisfy the given condition. The filtered elements are returned as a new collection. The original collection is not modified. It returns a list of elements that match the specified predicate.
  2. filterNot function: It retains the elements that do not satisfy the given condition. The filtered elements are returned as a new collection. The original collection is not modified. It returns a list of elements that do not match the specified predicate.

In simpler terms, filter keeps the elements that match the condition, while filterNot keeps the elements that do not match the condition.