Add Multiple Entries to HashMap in Kotlin | Kotlin HashMap putAll Function

The putAll function in Kotlin is used to copy all of the mappings from the specified map to the current HashMap. This function is part of the Kotlin standard library and provides a convenient way to add multiple entries to a map in one operation.

Table of Contents

  1. Introduction
  2. putAll Function Syntax
  3. Understanding putAll
  4. Examples
    • Basic Usage
    • Merging Two Maps
  5. Real-World Use Case
  6. Conclusion

Introduction

The putAll function allows you to copy all key-value pairs from one map to another. If the current HashMap already contains some of the keys, their values will be updated with the values from the specified map.

putAll Function Syntax

The syntax for the putAll function is as follows:

fun putAll(from: Map<out K, V>)

Parameters:

  • from: The map from which key-value pairs are to be copied.

Returns:

  • This function does not return any value.

Understanding putAll

The putAll function copies all key-value pairs from the specified map to the current HashMap. If a key already exists in the current map, the value will be updated with the value from the specified map. If a key does not exist, a new key-value pair will be added.

Examples

Basic Usage

To demonstrate the basic usage of putAll, we will create two HashMaps and copy the entries from one map to another.

Example

fun main() {
    val map1 = hashMapOf(
        "Alice" to 30,
        "Bob" to 25
    )
    val map2 = hashMapOf(
        "Charlie" to 35,
        "David" to 40
    )

    println("Map1 before putAll: $map1")
    map1.putAll(map2)
    println("Map1 after putAll: $map1")
}

Output:

Map1 before putAll: {Alice=30, Bob=25}
Map1 after putAll: {Alice=30, Bob=25, Charlie=35, David=40}

Merging Two Maps

This example shows how to merge two maps using the putAll function.

Example

fun main() {
    val map1 = hashMapOf(
        "Apple" to 3,
        "Banana" to 5
    )
    val map2 = hashMapOf(
        "Banana" to 6,
        "Cherry" to 7
    )

    println("Map1 before putAll: $map1")
    println("Map2: $map2")
    map1.putAll(map2)
    println("Map1 after putAll: $map1")
}

Output:

Map1 before putAll: {Apple=3, Banana=5}
Map2: {Banana=6, Cherry=7}
Map1 after putAll: {Apple=3, Banana=6, Cherry=7}

Real-World Use Case

Updating User Data in a Map

In real-world applications, the putAll function can be used to update user data stored in a HashMap by copying new entries or updates from another map.

Example

data class User(val id: Int, val name: String, val email: String)

fun main() {
    val userMap = hashMapOf(
        1 to User(1, "Alice", "alice@example.com"),
        2 to User(2, "Bob", "bob@example.com")
    )
    val newUserData = hashMapOf(
        2 to User(2, "Bob", "bob.new@example.com"),
        3 to User(3, "Charlie", "charlie@example.com")
    )

    println("User map before putAll: $userMap")
    userMap.putAll(newUserData)
    println("User map after putAll: $userMap")
}

Output:

User map before putAll: {1=User(id=1, name=Alice, email=alice@example.com), 2=User(id=2, name=Bob, email=bob@example.com)}
User map after putAll: {1=User(id=1, name=Alice, email=alice@example.com), 2=User(id=2, name=Bob, email=bob.new@example.com), 3=User(id=3, name=Charlie, email=charlie@example.com)}

Conclusion

The putAll function in Kotlin is a powerful and flexible way to copy all key-value pairs from one map to another. It allows you to efficiently merge or update maps, making it useful for various applications, including data management and user information handling. 

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

Comments