🎓 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 a HashSet. This function is part of the Kotlin standard library and provides a convenient way to verify the existence of an element in a set.
Table of Contents
- Introduction
containsFunction Syntax- Understanding
contains - Examples
- Basic Usage
- Checking for Different Element Types
- Real-World Use Case
- Conclusion
Introduction
The contains function allows you to check if a specified element is present in a HashSet. This is useful for scenarios where you need to verify the presence of an element before performing operations based on that element.
contains Function Syntax
The syntax for the contains function is as follows:
operator fun contains(element: E): Boolean
Parameters:
element: The element to be checked for presence in the set.
Returns:
Boolean: Returnstrueif the specified element is present in the set,falseotherwise.
Understanding contains
The contains function checks if the specified element is present in the HashSet. If the element is found, it returns true; otherwise, it returns false.
Examples
Basic Usage
To demonstrate the basic usage of contains, we will create a HashSet and check if specific elements are present in the set.
Example
fun main() {
val set = hashSetOf("Apple", "Banana", "Cherry")
val hasApple = set.contains("Apple")
val hasDate = set.contains("Date")
println("Does the set contain 'Apple'? $hasApple")
println("Does the set contain 'Date'? $hasDate")
}
Output:
Does the set contain 'Apple'? true
Does the set contain 'Date'? false
Checking for Different Element Types
This example shows how to use contains to check for the presence of different types of elements in a HashSet.
Example
fun main() {
val set = hashSetOf(1, 2, 3, 4, 5)
val hasThree = set.contains(3)
val hasSix = set.contains(6)
println("Does the set contain 3? $hasThree")
println("Does the set contain 6? $hasSix")
}
Output:
Does the set contain 3? true
Does the set contain 6? false
Real-World Use Case
Checking for Active Users
In real-world applications, the contains function can be used to check if a user is in a set of active users.
Example
fun main() {
val activeUsers = hashSetOf("user1", "user2", "user3")
val userToCheck = "user2"
if (activeUsers.contains(userToCheck)) {
println("$userToCheck is an active user.")
} else {
println("$userToCheck is not an active user.")
}
}
Output:
user2 is an active user.
Conclusion
The contains function in Kotlin is a simple and effective way to check if a specified element is present in a HashSet. It allows you to verify the presence of elements, making it useful for various applications, including data validation and session management.
By understanding and using the contains function, you can effectively manage and manipulate HashSet 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