Kotlin Sequence toMap Function

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

Table of Contents

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

Introduction

The toMap function allows you to convert a sequence into a map, which is a collection of key-value pairs. This is useful for scenarios where you need to transform a sequence into a dictionary-like structure for efficient lookups and associations.

toMap Function Syntax

The syntax for the toMap function is as follows:

fun <K, V> Sequence<Pair<K, V>>.toMap(): Map<K, V> fun <T, K, V> Sequence<T>.toMap(keySelector: (T) -> K, valueSelector: (T) -> V): Map<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 Map containing the key-value pairs from the sequence.

Understanding toMap

The toMap 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 map.

Examples

Basic Usage

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

Example

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

Output:

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

Converting a Sequence of Strings to a Map

This example shows how to convert a sequence of strings into a 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.toMap({ 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 toMap with Custom Objects

You can use the toMap function to convert a sequence of custom objects into a 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.toMap({ 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 toMap with Other Functions

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

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val evenNumberMap = numbers.filter { it % 2 == 0 } .toMap({ 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 Map

In real-world applications, the toMap function can be used to convert a sequence of products into a 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.toMap({ 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 toMap function in Kotlin provides used for converting a sequence into a map, ensuring that you can efficiently transform sequences into key-value pair collections according to your requirements. By understanding and using the toMap function, you can manage and process data in your Kotlin applications more effectively.

Leave a Comment

Scroll to Top