Kotlin Sequence toCollection Function

The toCollection function in Kotlin is used to convert a sequence into a specified collection. It is part of the Kotlin standard library and provides a way to transform a sequence into any collection type of your choice, such as a list, set, or queue.

Table of Contents

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

Introduction

The toCollection function allows you to convert a sequence into any collection type you specify. This is useful for scenarios where you need a specific type of collection to store the elements of a sequence.

toCollection Function Syntax

The syntax for the toCollection function is as follows:

fun <T, C : MutableCollection<in T>> Sequence<T>.toCollection(destination: C): C 

Parameters:

  • destination: The collection type to which the elements of the sequence will be added.

Returns:

  • The specified collection containing all elements from the sequence.

Understanding toCollection

The toCollection function works by iterating through the sequence and adding each element to the specified collection. The function returns the destination collection containing all elements from the sequence.

Examples

Basic Usage

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

Example

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

Output:

[1, 2, 3, 4, 5] 

Converting a Sequence of Strings to a List

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

Example

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

Output:

[Arjun, Bhaskar, Chitra, Deepak, Esha] 

Using toCollection with Custom Objects

You can use the toCollection function to convert a sequence of custom objects into a specified collection.

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 peopleSet = people.toCollection(HashSet()) println(peopleSet) // 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)] } 

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)] 

Chaining toCollection with Other Functions

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

Example

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

Output:

[2, 4, 6, 8, 10] 

Real-World Use Case

Converting a Sequence of Products to a Queue

In real-world applications, the toCollection function can be used to convert a sequence of products into a PriorityQueue to manage product processing in a specific order.

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 productQueue = products.toCollection(PriorityQueue(compareBy { it.price })) println(productQueue) // Output: [Product(name=Headphones, price=150.0), Product(name=Tablet, price=300.0), Product(name=Smartphone, price=600.0), Product(name=Laptop, price=1000.0)] } 

Output:

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

Conclusion

The toCollection function in Kotlin provides used for converting a sequence into any specified collection. By understanding and using the toCollection function, you can efficiently manage and process data in your Kotlin applications, ensuring that collections of elements are transformed according to your requirements.

Leave a Comment

Scroll to Top