Kotlin Sequence toMutableSet Function

The toMutableSet function in Kotlin is used to convert a sequence into a MutableSet. It is part of the Kotlin standard library and provides a way to transform a sequence into a mutable collection of unique elements.

Table of Contents

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

Introduction

The toMutableSet function allows you to convert a sequence into a MutableSet, which is an unordered collection that contains no duplicate elements and allows modifications. This is useful for scenarios where you need a collection of unique elements that can be modified after creation.

toMutableSet Function Syntax

The syntax for the toMutableSet function is as follows:

fun <T> Sequence<T>.toMutableSet(): MutableSet<T> 

Returns:

  • A MutableSet containing all unique elements from the sequence.

Understanding toMutableSet

The toMutableSet function works by iterating through the sequence and adding each element to a MutableSet. Since MutableSet does not allow duplicate elements, only unique elements from the sequence are added to the resulting set. The set is mutable, meaning you can add, remove, and update elements after its creation.

Examples

Basic Usage

To demonstrate the basic usage of toMutableSet, we will create a sequence of integers and convert it into a MutableSet.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 2, 1, 4, 5) val mutableSet = numbers.toMutableSet() println(mutableSet) // Output: [1, 2, 3, 4, 5] } 

Output:

[1, 2, 3, 4, 5] 

Converting a Sequence of Strings to a MutableSet

This example shows how to convert a sequence of strings into a MutableSet.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Arjun", "Esha") val nameSet = names.toMutableSet() println(nameSet) // Output: [Arjun, Bhaskar, Chitra, Esha] } 

Output:

[Arjun, Bhaskar, Chitra, Esha] 

Using toMutableSet with Custom Objects

You can use the toMutableSet function to convert a sequence of custom objects into a MutableSet.

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("Arjun", 25), Person("Esha", 26) ) val peopleSet = people.toMutableSet() println(peopleSet) // Output: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=22), Person(name=Esha, age=26)] } 

Output:

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

Chaining toMutableSet with Other Functions

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

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val evenNumberSet = numbers.filter { it % 2 == 0 } .toMutableSet() println(evenNumberSet) // Output: [2, 4, 6, 8, 10] } 

Output:

[2, 4, 6, 8, 10] 

Real-World Use Case

Converting a Sequence of Products to a MutableSet

In real-world applications, the toMutableSet function can be used to convert a sequence of products into a MutableSet to ensure that there are no duplicate products in the collection and that the collection can be modified as needed.

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("Smartphone", 600.0) ) val productSet = products.toMutableSet() println(productSet) // Output: [Product(name=Laptop, price=1000.0), Product(name=Smartphone, price=600.0), Product(name=Tablet, price=300.0)] } 

Output:

[Product(name=Laptop, price=1000.0), Product(name=Smartphone, price=600.0), Product(name=Tablet, price=300.0)] 

Conclusion

The toMutableSet function in Kotlin provides used for converting a sequence into a MutableSet, ensuring that only unique elements are included in the resulting set and that the set can be modified after creation. By understanding and using the toMutableSet function, you can efficiently manage and process data in your Kotlin applications, ensuring that collections of elements are unique and mutable according to your requirements.

Leave a Comment

Scroll to Top