This article explores different ways to convert a list to an array in Kotlin.

1. Using toTypedArray() function

List interface provides a toTypedArray() function that returns a typed array containing the list elements.

Download Code

 
To convert between two types, you can use the map() function. For example, the following code converts from a list of Integer to an array of String:

Download Code

2. Using Java 8 Stream

You can also use Stream to convert a list to an array. The trick is to convert the given list to Stream using the stream() function and use the toArray() function to return an array containing the Stream elements. The conversion between the two types can be done using a lambda expression.

Download Code

That’s all about converting a list to an array in Kotlin.