In this tutorial, we’ll explore how to convert an Array into a List in Kotlin. Though in practice these two data structures differ significantly in performance and memory footprint, in practice there is less of a difference. The reason for it is that most of the time when we need a List we use an ArrayList anyway. Let’s also remember that Kotlin encourages its users not to modify data structures, but to create new ones on update, and the difference becomes really scant.
Nevertheless, some of Kotlin standard library functions apply only to Collection descendants, so it’s helpful to know how to perform the conversion.
2. Convert by Wrapping
ArrayList is a wrapper over an array implementing the List interface. It makes perfect sense simply to wrap the array that we have as an argument into such a wrapper in order to make it a List.
The simplest way to wrap an array into a List is asList() function:
val array = intArrayOf(1, 2, 3, 4) val list = array.asList() assertEquals(listOf(1, 2, 3, 4), list)
For object arrays, this function delegates the actual execution to the Java Arrays.asList() function. For primitive type arrays it returns an ad-hoc object implementing AbstractList interface. The new list is backed by the original array. So, any change on the array will affect the converted list:
array = 0 assertEquals(listOf(0, 2, 3, 4), list)
Also, the conversion algorithm runs in O(1) from both time and space complexity perspectives.
3. Convert by Copying
Another logical way to convert Array to List is to copy all elements to a new data structure. Kotlin provides several ways to do it, and we can choose if we want the resultant list to be mutable or not.
3.1. Using the Array.toCollection Method
Kotlin provides an extension function on the Array class: Array.toCollection. This function requires a MutableCollection object as the parameter and appends all elements in the given array to the collection.
If we want to convert an array to a new mutableList, we can simply pass an empty MutableList object to the function, for instance, an ArrayList:
val myList = givenArray.toCollection(ArrayList()) assertThat(myList).isEqualTo(expectedList)
Of course, this function fits better if we would like to append elements from that array to an existing MutableList:
val existingList = mutableListOf("I have an element already.") val appendedList = givenArray.toCollection(existingList) assertThat(appendedList).isEqualTo(mutableListOf("I have an element already.", *givenArray))
Finally, we should note that the object returned by the toCollection function is the same object we pass to it. Thus, the returned list is mutable.
3.2. Using the Array.toList Method
val array = intArrayOf(1, 2, 3, 4) val list = array.toList() assertEquals(listOf(1, 2, 3, 4), list)
Since it’s a List, we won’t be able to mutate it after conversion. If we need to modify the list later on, we should use the toMutableList() extension function:
val array = intArrayOf(1, 2, 3, 4) val mutable = array.toMutableList() assertEquals(listOf(1, 2, 3, 4), mutable) mutable.add(5) assertEquals(listOf(1, 2, 3, 4, 5), mutable)
As shown above, after conversion, we added one element to the converted list. This is possible because this function returns an instance of MutableList instead of a plain List.
3.3. Using the listOf Method
Finally, there is technically a third way to copy an array to a new list. In Kotlin, the spread operator (*) can decompose an array instance to a vararg. The listOf and mutableListOf() functions accept vararg elements as the parameter. So we can create a test method using these functions:
val myList = listOf(*givenArray) assertThat(myList).isEqualTo(expectedList)
In the example, the resultant list is immutable. If we need a mutable list, we can call mutableListOf instead.
3.4. Link and Complexity
All these functions are copying the array values to the new list. Therefore, there are no lingering links between the array and the list after the conversion. That means if we change the array elements, that change won’t be reflected on the converted list:
array = 0 assertEquals(listOf(1, 2, 3, 4), list) // list didn't change
Moreover, the time and space complexity for the conversion algorithm would be O(n), as we need to copy all elements from the source array.
We saw how to convert a Kotlin Array to its corresponding List or MutableList. Depending on future use, it might be enough to wrap our array to make it behave like a List. If we want to add more elements to the List or be able to independently change the original Array, then it’s better to use a copying function. We can also join elements from an array to an existing collection.
As always, the full source code used in the article can be found over on GitHub.