🎓 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 containsValue function in Kotlin is used to check if a specified value 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 value in a map.
Table of Contents
- Introduction
containsValueFunction Syntax- Examples
- Basic Usage
- Checking for Different Value Types
- Real-World Use Case
- Conclusion
Introduction
The containsValue function allows you to check if a specified value is present in a HashMap. This is useful for scenarios where you need to verify the presence of a value before performing operations based on that value.
containsValue Function Syntax
The syntax for the containsValue function is as follows:
fun containsValue(value: V): Boolean
Parameters:
value: The value to be checked for presence in the map.
Returns:
Boolean: Returnstrueif the specified value is present in the map,falseotherwise.
The containsValue function checks if the specified value is present in the HashMap by comparing the value with the values in the map. If a match is found, it returns true; otherwise, it returns false.
Examples
Basic Usage
To demonstrate the basic usage of containsValue, we will create a HashMap and check if specific values 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 value 30? ${map.containsValue(30)}")
println("Does the map contain value 40? ${map.containsValue(40)}")
}
Output:
Does the map contain value 30? true
Does the map contain value 40? false
Checking for Different Value Types
This example shows how to use containsValue to check for the presence of different types of values in a HashMap.
Example
fun main() {
val map = hashMapOf(
1 to "One",
2 to "Two",
3 to "Three"
)
println("Does the map contain value 'Two'? ${map.containsValue("Two")}")
println("Does the map contain value 'Four'? ${map.containsValue("Four")}")
}
Output:
Does the map contain value 'Two'? true
Does the map contain value 'Four'? false
Real-World Use Case
Checking User Roles
In real-world applications, the containsValue function can be used to check if a specific role is assigned to any user in a HashMap.
Example
data class User(val id: Int, val name: String)
fun main() {
val userRoles = hashMapOf(
User(1, "Alice") to "Admin",
User(2, "Bob") to "Editor",
User(3, "Charlie") to "Viewer"
)
val roleToCheck = "Editor"
if (userRoles.containsValue(roleToCheck)) {
println("There is at least one user with the role: $roleToCheck")
} else {
println("No user has the role: $roleToCheck")
}
}
Output:
There is at least one user with the role: Editor
Conclusion
The containsValue function in Kotlin is a simple and effective way to check if a specified value is present in a HashMap. It allows you to verify the presence of values, making it useful for various applications, including data validation, user roles, and more.
By understanding and using the containsValue 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