The contains
function in Kotlin is used to check if a specified element exists in an array. This function is part of the Kotlin standard library and provides a straightforward way to verify the presence of elements in an array.
Table of Contents
- Introduction
contains
Function Syntax- Understanding
contains
- Examples
- Basic Usage
- Using
contains
with Custom Types - Case Sensitivity
- Real-World Use Case
- Conclusion
Introduction
The contains
function returns a boolean value indicating whether the specified element is present in the array. It is a simple and effective way to check for the existence of elements in an array.
contains Function Syntax
The syntax for the contains
function is as follows:
operator fun <T> Array<out T>.contains(element: T): Boolean
Parameters:
element
: The element to search for in the array.
Returns:
true
if the specified element is found in the array,false
otherwise.
Understanding contains
The contains
function searches the array from the beginning to the end and returns true
if the specified element is found. If the element is not found, it returns false
.
Examples
Basic Usage
To demonstrate the basic usage of contains
, we will create an array of integers and check if a specified element is present in the array.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val containsThree = numbers.contains(3)
println("Array contains 3: $containsThree")
}
Output:
Array contains 3: true
Using contains
with Custom Types
This example shows how to use contains
to check for the presence of a specified element in an array of custom objects. The custom objects must implement the equals
method to be comparable.
Example
data class Person(val name: String, val age: Int)
fun main() {
val people = arrayOf(
Person("Ravi", 25),
Person("Anjali", 30),
Person("Priya", 22)
)
val containsAnjali = people.contains(Person("Anjali", 30))
println("Array contains Anjali: $containsAnjali")
}
Output:
Array contains Anjali: true
Case Sensitivity
This example demonstrates how case sensitivity affects the contains
function when checking for string elements.
Example
fun main() {
val words = arrayOf("Kotlin", "is", "awesome")
val containsKotlin = words.contains("kotlin")
println("Array contains 'kotlin' (case-sensitive): $containsKotlin")
}
Output:
Array contains 'kotlin' (case-sensitive): false
Real-World Use Case
Checking for the Presence of an Item in a List
In real-world applications, the contains
function can be used to verify the presence of items in a list, such as checking if a specific product is available in a list of products.
Example
data class Product(val name: String, val price: Double)
fun main() {
val products = arrayOf(
Product("Laptop", 999.99),
Product("Smartphone", 699.99),
Product("Tablet", 299.99)
)
val containsSmartphone = products.contains(Product("Smartphone", 699.99))
println("Array contains Smartphone: $containsSmartphone")
}
Output:
Array contains Smartphone: true
Conclusion
The contains
function in Kotlin is a convenient method for checking if a specified element exists in an array. It provides a simple way to verify the presence of elements and handle cases where the element may or may not be found.
By understanding and using this function, you can effectively manage search operations in your Kotlin applications.
Comments
Post a Comment
Leave Comment