filter, map, and find on Collections
Apply lambdas to transform and filter collections using standard library functions.
filter, map, and find on Collections is a free Kotlin Academy lesson on CoddyKit — lesson 4 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.
Three Workhorse Functions
filter keeps elements matching a predicate. map transforms each element. find returns the first matching element (or null). Together they cover the bulk of collection processing.
filter Basics
filter returns a new list containing only elements for which the predicate is true.
fun main() {
val nums = listOf(1, 2, 3, 4, 5, 6)
val evens = nums.filter { it % 2 == 0 }
println(evens) // [2, 4, 6]
}map Basics
map returns a new list of the same size where each element is transformed by the lambda.
fun main() {
val nums = listOf(1, 2, 3)
val squares = nums.map { it * it }
println(squares) // [1, 4, 9]
}find Basics
find returns the first element matching the predicate, or null if none match.
fun main() {
val words = listOf("apple", "banana", "cherry")
val match = words.find { it.startsWith("b") }
println(match) // banana
}Chaining filter and map
Combine them with dot syntax to build expressive pipelines.
fun main() {
val nums = listOf(1, 2, 3, 4, 5)
val result = nums.filter { it % 2 == 1 }.map { it * 10 }
println(result) // [10, 30, 50]
}Filtering Data Classes
filter and map shine with collections of data class instances.
data class User(val name: String, val age: Int)
fun main() {
val users = listOf(User("Ana", 17), User("Ben", 25), User("Cal", 31))
val adultNames = users.filter { it.age >= 18 }.map { it.name }
println(adultNames) // [Ben, Cal]
}find vs first
find returns nullable; first { ... } throws if no match. Use find for optional matches.
fun main() {
val nums = listOf(1, 2, 3)
println(nums.find { it > 10 }) // null
// nums.first { it > 10 } // throws NoSuchElementException
println(nums.firstOrNull { it > 10 }) // null (alias of find)
}filterNot and filterNotNull
filterNot inverts the predicate. filterNotNull removes null entries from a list of nullables.
fun main() {
val nums = listOf(1, 2, 3, 4)
println(nums.filterNot { it % 2 == 0 }) // [1, 3]
val maybe: List<String?> = listOf("a", null, "b", null, "c")
println(maybe.filterNotNull()) // [a, b, c]
}mapNotNull
mapNotNull transforms then removes nulls — handy for parsing.
fun main() {
val raw = listOf("1", "two", "3", "four")
val numbers = raw.mapNotNull { it.toIntOrNull() }
println(numbers) // [1, 3]
}Real-World Pipeline
A small example combining filter, map, and a final reduction.
data class Product(val name: String, val price: Double, val inStock: Boolean)
fun main() {
val products = listOf(
Product("Pen", 1.0, true),
Product("Book", 12.0, false),
Product("Lamp", 25.0, true)
)
val total = products
.filter { it.inStock }
.map { it.price }
.sum()
println("Available total: $total") // 26.0
}Choosing Between Operations
Use filter when you want a subset, map to transform, find for a single optional match, and chain them when needed.
fun main() {
val words = listOf("alpha", "beta", "gamma", "delta")
println(words.filter { it.length == 5 }) // [alpha, gamma, delta]
println(words.map { it.uppercase() }) // [ALPHA, BETA, GAMMA, DELTA]
println(words.find { it.endsWith("ta") }) // beta
}Quick Check
Which function returns a new collection with elements transformed by a lambda?
Recap
filter selects, map transforms, find locates the first match (or null). Combine them freely in pipelines; reach for mapNotNull and filterNotNull when handling optional values.
Frequently asked questions
Is the “filter, map, and find on Collections” lesson free?
Yes — the full text of “filter, map, and find on Collections” 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 “filter, map, and find on Collections”?
Apply lambdas to transform and filter collections using standard library functions. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “filter, map, and find on Collections” 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
- What Is a Lambda? Syntax and Types
- Passing Lambdas to Functions
- it: The Implicit Parameter
- filter, map, and find on Collections