Check if a value exists in a Kotlin List
This article explores different ways to check if a value exists in a Kotlin List.
1. Using in operator
The shortest and most idiomatic way to check whether a List contains a given value is using the in operator. It can be used as follows:
|
1 2 3 4 5 6 7 |
fun main() { val values = listOf(1, 2, 3, 4, 5) val value = 3 val result = value in values println(result) // true } |
Note that in operator is equivalent to calling the contains() function. The expression a in b is translated to a.contains(b).
|
1 2 3 4 5 6 7 |
fun main() { val values = listOf(1, 2, 3, 4, 5) val value = 3 val result = values.contains(value) println(result) // true } |
For frequent calls, it’s better to convert the list into a set and call the contains() function on it.
|
1 2 3 4 5 6 7 |
fun main() { val values = listOf(1, 2, 3, 4, 5) val value = 3 val result = values.toMutableSet().contains(value) println(result) // true } |
For a list of custom objects, don’t forget to override the equals and hashCode functions. For a data class, this is not needed, as it automatically derives the equals and hashCode.
2. Using filter() function
Alternatively, you can use the filter() function to filter the given value in the list and check if the resultant list is not empty. Here’s a working example:
|
1 2 3 4 5 6 7 8 9 10 11 |
fun find(values: List<Int>, value: Int): Boolean { return values.filter { it == value }.isNotEmpty() } fun main() { val values = listOf(1, 2, 3, 4, 5) val value = 3 val result = find(values, value) println(result) // true } |
You may also use the any() extension function to conditionally search a value in a List:
|
1 2 3 4 5 6 7 8 9 10 11 |
fun find(values: List<Int>, value: Int): Boolean { return values.any { it == value } } fun main() { val values = listOf(1, 2, 3, 4, 5) val value = 3 val result = find(values, value) println(result) // true } |
To search for multiple items in the list, you can do like:
|
1 2 3 4 5 6 7 8 9 10 |
fun find(values: List<Int>, vararg itemsToSearch: Int): Boolean { return values.any { it in itemsToSearch } } fun main() { val values = listOf(1, 2, 3, 4, 5) val result = find(values, 4, 6) println(result) // true } |
The code can be shortened by making use of the functional reference for the in operator:
|
1 2 3 4 5 6 7 8 9 10 |
fun find(values: List<Int>, vararg itemsToSearch: Int): Boolean { return itemsToSearch.any(values::contains) } fun main() { val values = listOf(1, 2, 3, 4, 5) val result = find(values, 4, 6) println(result) // true } |
3. Using Loop
Finally, you can write your custom logic for searching a value in the list using a loop. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
fun find(values: List<Int>, value: Int): Boolean { for (e in values) { if (e == value) { return true } } return false } fun main() { val values = listOf(1, 2, 3, 4, 5) val value = 3 val result = find(values, value) println(result) // true } |
That’s all about checking if a value exists in a Kotlin List.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)