Clear All Entries from HashMap in Kotlin | Kotlin HashMap clear Function

The clear function in Kotlin is used to remove all key-value pairs from a HashMap. This function is part of the Kotlin standard library and provides a convenient way to empty a map.

Table of Contents

  1. Introduction
  2. clear Function Syntax
  3. Understanding clear
  4. Examples
    • Basic Usage
    • Clearing a Non-Empty HashMap
  5. Real-World Use Case
  6. Conclusion

Introduction

The clear function allows you to remove all entries from a HashMap, leaving it empty. This is useful for scenarios where you need to reset or reuse a HashMap without creating a new instance.

clear Function Syntax

The syntax for the clear function is as follows:

fun clear()

Parameters:

  • This function does not take any parameters.

Returns:

  • This function does not return any value.

Understanding clear

The clear function removes all key-value pairs from the HashMap, resulting in an empty map. The size of the map after calling clear will be 0.

Examples

Basic Usage

To demonstrate the basic usage of clear, we will create a HashMap, add some entries, and then clear the map.

Example

fun main() {
    val map = hashMapOf(
        "Alice" to 30,
        "Bob" to 25,
        "Charlie" to 35
    )
    println("Original map: $map")

    map.clear()
    println("Map after clear: $map")
}

Output:

Original map: {Alice=30, Bob=25, Charlie=35}
Map after clear: {}

Clearing a Non-Empty HashMap

This example shows how to clear a non-empty HashMap and verify that it is empty afterward.

Example

fun main() {
    val fruits = hashMapOf(
        "Apple" to 3,
        "Banana" to 5,
        "Cherry" to 7
    )
    println("Original map: $fruits")

    fruits.clear()
    println("Map after clear: $fruits")
    println("Is the map empty? ${fruits.isEmpty()}")
}

Output:

Original map: {Apple=3, Banana=5, Cherry=7}
Map after clear: {}
Is the map empty? true

Real-World Use Case

Resetting a Map of User Sessions

In real-world applications, the clear function can be used to reset a map of user sessions, allowing you to start fresh without creating a new map instance.

Example

data class Session(val userId: String, val token: String)

fun main() {
    val sessions = hashMapOf(
        "session1" to Session("user1", "token1"),
        "session2" to Session("user2", "token2"),
        "session3" to Session("user3", "token3")
    )
    println("Original sessions: $sessions")

    sessions.clear()
    println("Sessions after clear: $sessions")
}

Output:

Original sessions: {session1=Session(userId=user1, token=token1), session2=Session(userId=user2, token=token2), session3=Session(userId=user3, token=token3)}
Sessions after clear: {}

Conclusion

The clear function in Kotlin is a simple and effective way to remove all key-value pairs from a HashMap. It allows you to reset or reuse a HashMap without creating a new instance, making it useful for various applications, including data processing and session management. 

By understanding and using the clear function, you can effectively manage and manipulate HashMap collections in your Kotlin applications.

Comments