Kotlin hashMapOf Function | Create HashMap in Kotlin

The hashMapOf function in Kotlin is used to create a mutable HashMap of key-value pairs. This function belongs to the Kotlin standard library and provides a straightforward way to create hash maps that can be modified after their creation.

Table of Contents

  1. Introduction
  2. hashMapOf Function Syntax
  3. Examples
    • Basic Usage
    • Adding and Removing Entries
    • Modifying Values
  4. Real-World Use Case
  5. Conclusion

Introduction

The hashMapOf function allows you to create a mutable HashMap containing specified key-value pairs. A HashMap is a collection that stores key-value pairs and allows for fast retrieval, addition, and removal of elements.

hashMapOf Function Syntax

The syntax for the hashMapOf function is as follows:

fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>

Parameters:

  • pairs: A variable number of pairs, where each pair consists of a key and a value.

Returns:

  • A mutable HashMap containing the specified key-value pairs.

Examples

Basic Usage

To demonstrate the basic usage of hashMapOf, we will create a mutable HashMap of integers and strings.

Example

fun main() {
    val numberMap = hashMapOf(1 to "One", 2 to "Two", 3 to "Three")
    println("HashMap of numbers: $numberMap")
}

Output:

HashMap of numbers: {1=One, 2=Two, 3=Three}

Adding and Removing Entries

This example shows how to add and remove entries in a HashMap.

Example

fun main() {
    val countryMap = hashMapOf("USA" to "Washington, D.C.", "France" to "Paris", "Japan" to "Tokyo")
    println("Original map: $countryMap")

    countryMap["Germany"] = "Berlin"
    println("After adding an entry: $countryMap")

    countryMap.remove("France")
    println("After removing an entry: $countryMap")
}

Output:

Original map: {USA=Washington, D.C., France=Paris, Japan=Tokyo}
After adding an entry: {USA=Washington, D.C., France=Paris, Japan=Tokyo, Germany=Berlin}
After removing an entry: {USA=Washington, D.C., Japan=Tokyo, Germany=Berlin}

Modifying Values

This example demonstrates how to modify values in a HashMap.

Example

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

    userMap["Bob"] = 26
    println("After modifying a value: $userMap")
}

Output:

Original map: {Alice=30, Bob=25, Charlie=35}
After modifying a value: {Alice=30, Bob=26, Charlie=35}

Real-World Use Case

Managing a Dynamic Collection of Product Data

In real-world applications, the hashMapOf function can be used to manage a dynamic collection of product data, such as adding, removing, and updating product information.

Example

fun main() {
    val products = hashMapOf("P001" to "Laptop", "P002" to "Smartphone")
    println("Original products: $products")

    products["P003"] = "Tablet"
    println("After adding a product: $products")

    products["P002"] = "Smartphone - Updated"
    println("After updating a product: $products")

    products.remove("P001")
    println("After removing a product: $products")
}

Output:

Original products: {P001=Laptop, P002=Smartphone}
After adding a product: {P001=Laptop, P002=Smartphone, P003=Tablet}
After updating a product: {P001=Laptop, P002=Smartphone - Updated, P003=Tablet}
After removing a product: {P002=Smartphone - Updated, P003=Tablet}

Conclusion

The hashMapOf function in Kotlin is a powerful and convenient way to create mutable HashMaps. It allows you to define a collection of key-value pairs that can be dynamically updated, making it suitable for various applications, including managing dynamic data collections. 

By understanding and using the hashMapOf function, you can effectively manage mutable hash maps in your Kotlin applications.

Comments