0
0
KotlinHow-ToBeginner · 3 min read

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): Returns true if the element is in the list.
  • list.find { condition }: Returns the first element matching the condition or null if none.
  • list.indexOf(element): Returns the index of the element or -1 if 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 handling null when no element matches.
  • Confusing contains() with find()contains() returns a Boolean, find() returns the element or null.
  • Using indexOf() which returns -1 if 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

MethodDescriptionReturn TypeExample
contains(element)Checks if element existsBooleanlist.contains("apple") // true
find { condition }Finds first element matching conditionElement or nulllist.find { it.startsWith("b") } // "banana"
indexOf(element)Finds index of elementInt (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.