Kotlin Sequence toSortedMap Function

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

Table of Contents

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

Introduction

The toSortedMap function allows you to convert a sequence into a SortedMap, which is a collection of key-value pairs sorted by keys. This is useful for scenarios where you need a sorted dictionary-like structure for efficient lookups and ordered associations.

toSortedMap Function Syntax

The syntax for the toSortedMap function is as follows:

fun <K : Comparable<K>, V> Sequence<Pair<K, V>>.toSortedMap(): SortedMap<K, V> fun <T, K : Comparable<K>, V> Sequence<T>.toSortedMap(keySelector: (T) -> K, valueSelector: (T) -> V): SortedMap<K, V> fun <K, V> Sequence<Pair<K, V>>.toSortedMap(comparator: Comparator<in K>): SortedMap<K, V> fun <T, K, V> Sequence<T>.toSortedMap(comparator: Comparator<in K>, keySelector: (T) -> K, valueSelector: (T) -> V): SortedMap<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.
  • comparator: A comparator to define the order of the keys in the sorted map.

Returns:

  • A SortedMap containing the key-value pairs from the sequence, sorted by keys.

Understanding toSortedMap

The toSortedMap 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 sorted map, which orders the pairs by their keys either naturally or according to a provided comparator.

Examples

Basic Usage

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

Example

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

Output:

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

Converting a Sequence of Strings to a SortedMap

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

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

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

Example

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

In real-world applications, the toSortedMap function can be used to convert a sequence of products into a sorted 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.toSortedMap({ it.name }, { it.price }) println(productPriceMap) // Output: {Headphones=150.0, Laptop=1000.0, Smartphone=600.0, Tablet=300.0} } 

Output:

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

Conclusion

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

Leave a Comment

Scroll to Top