Kotlin Sequence toMutableList Function

The toMutableList function in Kotlin is used to convert a sequence into a mutable list. It is part of the Kotlin standard library and allows you to create a mutable list from the elements of a sequence, providing a convenient way to obtain a modifiable collection of elements.

Table of Contents

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

Introduction

The toMutableList function allows you to convert a sequence into a mutable list. This is useful for scenarios where you need a modifiable collection of elements, enabling you to perform operations such as adding, removing, or updating elements in the list.

toMutableList Function Syntax

The syntax for the toMutableList function is as follows:

fun <T> Sequence<T>.toMutableList(): MutableList<T>

Returns:

  • A mutable list containing all the elements from the sequence.

Understanding toMutableList

The toMutableList function takes a sequence and collects its elements into a mutable list. This operation is terminal, meaning it triggers the evaluation of the sequence and produces a concrete mutable list containing the elements.

Examples

Basic Usage

To demonstrate the basic usage of toMutableList, we will create a sequence of integers and convert it to a mutable list.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5)
    val numberList = numbers.toMutableList()
    numberList.add(6)
    println(numberList) // Output: [1, 2, 3, 4, 5, 6]
}

Output:

[1, 2, 3, 4, 5, 6]

Converting a Sequence of Strings to a Mutable List

This example shows how to convert a sequence of strings to a mutable list and modify it.

Example

fun main() {
    val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
    val nameList = names.toMutableList()
    nameList.remove("Chitra")
    nameList.add("Fahad")
    println(nameList) // Output: [Arjun, Bhaskar, Deepak, Esha, Fahad]
}

Output:

[Arjun, Bhaskar, Deepak, Esha, Fahad]

Using toMutableList with Custom Objects

You can use the toMutableList function to convert a sequence of custom objects to a mutable list and modify it.

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 personList = people.toMutableList()
    personList.add(Person("Fahad", 29))
    println(personList) // Output: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=22), Person(name=Deepak, age=28), Person(name=Esha, age=26), Person(name=Fahad, age=29)]
}

Output:

[Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=22), Person(name=Deepak, age=28), Person(name=Esha, age=26), Person(name=Fahad, age=29)]

Chaining toMutableList with Other Functions

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

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val resultList = numbers.filter { it % 2 == 0 }
                            .map { it * 2 }
                            .toMutableList()
    resultList.add(22)
    println(resultList) // Output: [4, 8, 12, 16, 20, 22]
}

Output:

[4, 8, 12, 16, 20, 22]

Real-World Use Case

Managing a List of Tasks

In real-world applications, the toMutableList function can be used to manage a list of tasks or items that need to be modified dynamically.

Example

data class Task(val name: String, val priority: Int)

fun main() {
    val tasks = sequenceOf(
        Task("Buy groceries", 1),
        Task("Pay bills", 2),
        Task("Call mom", 3),
        Task("Clean house", 1)
    )

    val taskList = tasks.toMutableList()
    taskList.add(Task("Walk the dog", 2))
    taskList.removeAt(1)
    println(taskList) // Output: [Task(name=Buy groceries, priority=1), Task(name=Call mom, priority=3), Task(name=Clean house, priority=1), Task(name=Walk the dog, priority=2)]
}

Output:

[Task(name=Buy groceries, priority=1), Task(name=Call mom, priority=3), Task(name=Clean house, priority=1), Task(name=Walk the dog, priority=2)]

Conclusion

The toMutableList function in Kotlin provides used for converting sequences into mutable lists, enabling you to work with modifiable collections of elements. By understanding and using the toMutableList function, you can efficiently manage and process data in your Kotlin applications, ensuring that you have the flexibility to modify the collections according to your requirements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top