How to Find Element in List Kotlin: Simple Guide
In Kotlin, you can find an element in a list using
contains() to check if it exists, find() to get the first matching element, or indexOf() to find its position. These methods help you quickly locate elements based on value or condition.Syntax
Here are common ways to find elements in a Kotlin list:
list.contains(element): Returnstrueif the element is in the list.list.find { condition }: Returns the first element matching the condition ornullif none.list.indexOf(element): Returns the index of the element or-1if not found.
kotlin
val list = listOf("apple", "banana", "cherry") val hasBanana = list.contains("banana") val firstWithA = list.find { it.startsWith("a") } val indexCherry = list.indexOf("cherry")
Example
This example shows how to check if an element exists, find an element by condition, and get the index of an element in a Kotlin list.
kotlin
fun main() {
val fruits = listOf("apple", "banana", "cherry", "date")
// Check if "banana" is in the list
val hasBanana = fruits.contains("banana")
println("Contains banana: $hasBanana")
// Find first fruit starting with 'c'
val fruitWithC = fruits.find { it.startsWith("c") }
println("First fruit starting with 'c': $fruitWithC")
// Get index of "date"
val indexDate = fruits.indexOf("date")
println("Index of date: $indexDate")
// Try to find a fruit starting with 'z'
val fruitWithZ = fruits.find { it.startsWith("z") }
println("Fruit starting with 'z': $fruitWithZ")
}Output
Contains banana: true
First fruit starting with 'c': cherry
Index of date: 3
Fruit starting with 'z': null
Common Pitfalls
Some common mistakes when finding elements in Kotlin lists include:
- Using
find()without handlingnullwhen no element matches. - Confusing
contains()withfind()—contains()returns a Boolean,find()returns the element ornull. - Using
indexOf()which returns-1if the element is not found, so always check the result before using it as an index.
kotlin
fun main() {
val numbers = listOf(1, 2, 3)
// Wrong: assuming find() never returns null
val result = numbers.find { it > 5 }
println(result.toString()) // Prints null, can cause errors if not checked
// Correct: check for null
val safeResult = numbers.find { it > 5 } ?: "No match"
println(safeResult)
// Wrong: using indexOf without checking
val index = numbers.indexOf(10)
println(index) // Prints -1, invalid index
}Output
null
No match
-1
Quick Reference
| Method | Description | Return Type | Example |
|---|---|---|---|
| contains(element) | Checks if element exists | Boolean | list.contains("apple") // true |
| find { condition } | Finds first element matching condition | Element or null | list.find { it.startsWith("b") } // "banana" |
| indexOf(element) | Finds index of element | Int (or -1 if not found) | list.indexOf("cherry") // 2 |
Key Takeaways
Use contains() to check if a list has an element, returning true or false.
Use find() with a condition to get the first matching element or null if none.
Use indexOf() to get the position of an element, but check for -1 if not found.
Always handle null results from find() to avoid errors.
Remember indexOf() returns -1 when the element is missing, not a valid index.