🎓 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 containsKey function in Kotlin is used to check if a specified key is present in a HashMap. This function is part of the Kotlin standard library and provides a convenient way to verify the existence of a key in a map.
Table of Contents
- Introduction
containsKeyFunction Syntax- Understanding
containsKey - Examples
- Basic Usage
- Checking for Different Key Types
- Real-World Use Case
- Conclusion
Introduction
The containsKey function allows you to check if a specified key is present in a HashMap. This is useful for scenarios where you need to verify the presence of a key before performing operations based on that key.
containsKey Function Syntax
The syntax for the containsKey function is as follows:
fun containsKey(key: K): Boolean
Parameters:
key: The key to be checked for presence in the map.
Returns:
Boolean: Returnstrueif the specified key is present in the map,falseotherwise.
Understanding containsKey
The containsKey function checks if the specified key is present in the HashMap by comparing the key with the keys in the map. If a match is found, it returns true; otherwise, it returns false.
Examples
Basic Usage
To demonstrate the basic usage of containsKey, we will create a HashMap and check if specific keys are present in the map.
Example
fun main() {
val map = hashMapOf(
"Alice" to 30,
"Bob" to 25,
"Charlie" to 35
)
println("Does the map contain 'Alice'? ${map.containsKey("Alice")}")
println("Does the map contain 'David'? ${map.containsKey("David")}")
}
Output:
Does the map contain 'Alice'? true
Does the map contain 'David'? false
Checking for Different Key Types
This example shows how to use containsKey to check for the presence of different types of keys in a HashMap.
Example
fun main() {
val map = hashMapOf(
1 to "One",
2 to "Two",
3 to "Three"
)
println("Does the map contain key 2? ${map.containsKey(2)}")
println("Does the map contain key 4? ${map.containsKey(4)}")
}
Output:
Does the map contain key 2? true
Does the map contain key 4? false
Real-World Use Case
Checking User Permissions
In real-world applications, the containsKey function can be used to check if a user has specific permissions or roles in a HashMap.
Example
data class User(val id: Int, val name: String)
fun main() {
val userPermissions = hashMapOf(
User(1, "Alice") to "Admin",
User(2, "Bob") to "Editor",
User(3, "Charlie") to "Viewer"
)
val userToCheck = User(2, "Bob")
if (userPermissions.containsKey(userToCheck)) {
println("${userToCheck.name} has the role: ${userPermissions[userToCheck]}")
} else {
println("${userToCheck.name} does not have any role.")
}
}
Output:
Bob has the role: Editor
Conclusion
The containsKey function in Kotlin is a simple and effective way to check if a specified key is present in a HashMap. It allows you to verify the presence of keys, making it useful for various applications, including data validation, user permissions, and more.
By understanding and using the containsKey function, you can effectively manage and manipulate HashMap 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