Kotlin Sequence toMutableMap Function

The toMutableMap function in Kotlin is used to convert a sequence into a MutableMap. It is part of the Kotlin standard library and provides a way to transform a sequence into a mutable key-value pair collection.

Table of Contents

  1. Introduction
  2. toMutableMap Function Syntax
  3. Understanding toMutableMap
  4. Examples
    • Basic Usage
    • Converting a Sequence of Strings to a MutableMap
    • Using toMutableMap with Custom Objects
    • Chaining toMutableMap with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The toMutableMap function allows you to convert a sequence into a MutableMap, which is a collection of key-value pairs that allows modifications. This is useful for scenarios where you need a dictionary-like structure that can be updated after its creation.

toMutableMap Function Syntax

The syntax for the toMutableMap function is as follows:

fun <K, V> Sequence<Pair<K, V>>.toMutableMap(): MutableMap<K, V> fun <T, K, V> Sequence<T>.toMutableMap(keySelector: (T) -> K, valueSelector: (T) -> V): MutableMap<K, V> 

Parameters:

  • keySelector: A lambda function that defines how to select the key for each element.
  • valueSelector: A lambda function that defines how to select the value for each element.

Returns:

  • A MutableMap containing the key-value pairs from the sequence.

Understanding toMutableMap

The toMutableMap function works by iterating through the sequence and applying the keySelector and valueSelector functions to each element to generate key-value pairs. These pairs are then added to the resulting mutable map.

Examples

Basic Usage

To demonstrate the basic usage of toMutableMap, we will create a sequence of pairs and convert it into a mutable map.

Example

fun main() { val pairs = sequenceOf(1 to "One", 2 to "Two", 3 to "Three") val map = pairs.toMutableMap() println(map) // Output: {1=One, 2=Two, 3=Three} } 

Output:

{1=One, 2=Two, 3=Three} 

Converting a Sequence of Strings to a MutableMap

This example shows how to convert a sequence of strings into a mutable map where the key is the string and the value is the length of the string.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val nameLengthMap = names.toMutableMap({ it }, { it.length }) println(nameLengthMap) // Output: {Arjun=5, Bhaskar=7, Chitra=6, Deepak=6, Esha=4} } 

Output:

{Arjun=5, Bhaskar=7, Chitra=6, Deepak=6, Esha=4} 

Using toMutableMap with Custom Objects

You can use the toMutableMap function to convert a sequence of custom objects into a mutable map.

Example

data class Person(val name: String, val age: Int) fun main() { val people = sequenceOf( Person("Arjun", 25), Person("Bhaskar", 30), Person("Chitra", 22), Person("Deepak", 28), Person("Esha", 26) ) val peopleMap = people.toMutableMap({ it.name }, { it.age }) println(peopleMap) // Output: {Arjun=25, Bhaskar=30, Chitra=22, Deepak=28, Esha=26} } 

Output:

{Arjun=25, Bhaskar=30, Chitra=22, Deepak=28, Esha=26} 

Chaining toMutableMap with Other Functions

The toMutableMap function can be chained with other sequence functions to perform more complex operations before converting the sequence into a mutable map.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val evenNumberMap = numbers.filter { it % 2 == 0 } .toMutableMap({ it }, { it * it }) println(evenNumberMap) // Output: {2=4, 4=16, 6=36, 8=64, 10=100} } 

Output:

{2=4, 4=16, 6=36, 8=64, 10=100} 

Real-World Use Case

Converting a Sequence of Products to a MutableMap

In real-world applications, the toMutableMap function can be used to convert a sequence of products into a mutable map where the key is the product name and the value is the product price.

Example

data class Product(val name: String, val price: Double) fun main() { val products = sequenceOf( Product("Laptop", 1000.0), Product("Smartphone", 600.0), Product("Tablet", 300.0), Product("Headphones", 150.0) ) val productPriceMap = products.toMutableMap({ it.name }, { it.price }) println(productPriceMap) // Output: {Laptop=1000.0, Smartphone=600.0, Tablet=300.0, Headphones=150.0} } 

Output:

{Laptop=1000.0, Smartphone=600.0, Tablet=300.0, Headphones=150.0} 

Conclusion

The toMutableMap function in Kotlin provides used for converting a sequence into a mutable map, ensuring that you can efficiently transform sequences into key-value pair collections that can be modified according to your requirements. By understanding and using the toMutableMap function, you can manage and process data in your Kotlin applications more effectively.

Leave a Comment

Scroll to Top