The toArray function in Kotlin is used to convert a sequence into an array. It is part of the Kotlin standard library and provides a way to transform a sequence into an array of the same type as the elements in the sequence.
Table of Contents
- Introduction
toArrayFunction Syntax- Understanding
toArray - Examples
- Basic Usage
- Converting a Sequence of Strings to an Array
- Using
toArraywith Custom Objects - Chaining
toArraywith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The toArray function allows you to convert a sequence into an array, which is a collection of elements that can be accessed by their index. This is useful for scenarios where you need to transform a sequence into an array for efficient element access and manipulation.
toArray Function Syntax
The syntax for the toArray function is as follows:
inline fun <reified T> Sequence<T>.toArray(): Array<T>
Returns:
- An
Arraycontaining all elements from the sequence.
Understanding toArray
The toArray function works by iterating through the sequence and adding each element to an array. The function returns the resulting array containing all elements from the sequence.
Examples
Basic Usage
To demonstrate the basic usage of toArray, we will create a sequence of integers and convert it into an array.
Example
fun main() {
val numbers = sequenceOf(1, 2, 3, 4, 5)
val array = numbers.toArray()
println(array.joinToString()) // Output: 1, 2, 3, 4, 5
}
Output:
1, 2, 3, 4, 5
Converting a Sequence of Strings to an Array
This example shows how to convert a sequence of strings into an array.
Example
fun main() {
val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
val array = names.toArray()
println(array.joinToString()) // Output: Arjun, Bhaskar, Chitra, Deepak, Esha
}
Output:
Arjun, Bhaskar, Chitra, Deepak, Esha
Using toArray with Custom Objects
You can use the toArray function to convert a sequence of custom objects into an array.
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 array = people.toArray()
array.forEach { println(it) }
// 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 toArray with Other Functions
The toArray function can be chained with other sequence functions to perform more complex operations before converting the sequence into an array.
Example
fun main() {
val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val evenNumbersArray = numbers.filter { it % 2 == 0 }
.toArray()
println(evenNumbersArray.joinToString()) // Output: 2, 4, 6, 8, 10
}
Output:
2, 4, 6, 8, 10
Real-World Use Case
Converting a Sequence of Products to an Array
In real-world applications, the toArray function can be used to convert a sequence of products into an array for efficient manipulation and access.
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 productArray = products.toArray()
productArray.forEach { println(it) }
// Output:
// Product(name=Laptop, price=1000.0)
// Product(name=Smartphone, price=600.0)
// Product(name=Tablet, price=300.0)
// Product(name=Headphones, price=150.0)
}
Output:
Product(name=Laptop, price=1000.0)
Product(name=Smartphone, price=600.0)
Product(name=Tablet, price=300.0)
Product(name=Headphones, price=150.0)
Conclusion
The toArray function in Kotlin provides used for converting a sequence into an array, ensuring that you can efficiently transform sequences into indexed collections for easier manipulation and access. By understanding and using the toArray function, you can manage and process data in your Kotlin applications more effectively.