Check for Value in HashMap in Kotlin | Kotlin HashMap containsValue Function

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

  1. Introduction
  2. containsValue Function Syntax
  3. Examples
    • Basic Usage
    • Checking for Different Value Types
  4. Real-World Use Case
  5. 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: Returns true if the specified value is present in the map, false otherwise.

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.

Comments