🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The contains function in Kotlin is used to check if a specified element is present in an ArrayList. This function is part of the Kotlin standard library and provides a convenient way to determine if a list contains a particular element.
Table of Contents
- Introduction
containsFunction Syntax- Understanding
contains - Examples
- Basic Usage
- Checking for Different Types of Elements
- Real-World Use Case
- Conclusion
Introduction
The contains function allows you to check if a specified element is present in an ArrayList. This is useful for scenarios where you need to verify the existence of an element within a list.
contains Function Syntax
The syntax for the contains function is as follows:
fun <T> ArrayList<T>.contains(element: T): Boolean
Parameters:
element: The element to be checked for presence in the list.
Returns:
Boolean: Returnstrueif the element is present in the list,falseotherwise.
Understanding contains
The contains function checks if the specified element is present in the ArrayList by comparing each element in the list with the specified element. If a match is found, it returns true; otherwise, it returns false.
Examples
Basic Usage
To demonstrate the basic usage of contains, we will create an ArrayList and check if specific elements are present in the list.
Example
fun main() {
val numbers = arrayListOf(1, 2, 3, 4, 5)
println("Does the list contain 3? ${numbers.contains(3)}")
println("Does the list contain 6? ${numbers.contains(6)}")
}
Output:
Does the list contain 3? true
Does the list contain 6? false
Checking for Different Types of Elements
This example shows how to use contains to check for the presence of different types of elements in an ArrayList.
Example
fun main() {
val fruits = arrayListOf("Apple", "Banana", "Cherry")
val tasks = arrayListOf(
Task(1, "Do the laundry"),
Task(2, "Buy groceries"),
Task(3, "Write blog post")
)
println("Does the list contain 'Banana'? ${fruits.contains("Banana")}")
println("Does the list contain 'Date'? ${fruits.contains("Date")}")
println("Does the list contain Task(2, 'Buy groceries')? ${tasks.contains(Task(2, "Buy groceries"))}")
println("Does the list contain Task(4, 'Read a book')? ${tasks.contains(Task(4, "Read a book"))}")
}
data class Task(val id: Int, val description: String)
Output:
Does the list contain 'Banana'? true
Does the list contain 'Date'? false
Does the list contain Task(2, 'Buy groceries')? true
Does the list contain Task(4, 'Read a book')? false
Real-World Use Case
Checking User Permissions
In real-world applications, the contains function can be used to check if a user has specific permissions or roles.
Example
data class User(val id: Int, val name: String, val roles: ArrayList<String>)
fun main() {
val user = User(1, "Alice", arrayListOf("admin", "editor", "viewer"))
println("Is user an admin? ${user.roles.contains("admin")}")
println("Is user a contributor? ${user.roles.contains("contributor")}")
}
Output:
Is user an admin? true
Is user a contributor? false
Conclusion
The contains function in Kotlin is a simple and effective way to check if an ArrayList contains a specified element. It allows you to verify the presence of elements within a list, making it useful for various applications, including data validation, user permissions, and more.
By understanding and using the contains function, you can effectively manage and manipulate ArrayList collections in your Kotlin applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment