Ad space (banner)
๐ŸŽฏKotlin Lessons
Lesson 22 / 68

Searching a List (contains, find)

In this lesson you will learn to search a list in Kotlin with contains and find, so you can check whether a value is present. This is for anyone searching for "Kotlin list contains find how to use".

.contains(value) tests whether a value is present, and .find { } finds the first element matching a condition.

The sample code confirms presence with fruits.contains("orange") and looks for the first element beginning with "g" using fruits.find { it.startsWith("g") }. it is the implicit parameter name available inside a lambda - a concise, characteristically Kotlin touch.

A common stumbling block for beginners is performance: the more data there is to search, the longer it takes, so faster approaches are worth considering for large volumes.

In real-world development this search comes up whenever you need to confirm that a list contains a particular product ID or user name.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Kotlin directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)