Authors Top

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this quick tutorial, we’ll explore how to convert an Array into a List in Kotlin.

For simplicity, we’ll use assertions in unit tests to verify the conversion result.

2. Introduction to the Problem

When we work with Kotlin daily, we should prefer Lists over Arrays. Therefore, we may want to convert an array into a list.

Usually, we would like to get a new List object containing the same element with the same order in the array. However, depending on the requirement, we may want to append all elements in the given array to an existing list object.

In this tutorial, we’ll introduce a few approaches to cover these two scenarios.

We’ll take an Array<String> object as an example:

val givenArray = arrayOf("one", "two", "three", "four", "five")

Of course, if the conversion is successful, we should get a List object equal to this:

val expectedList = listOf("one", "two", "three", "four", "five")

Next, let’s see how to do the conversions.

3. Using Java’s Arrays.asList Method

Java standard library’s Arrays class has provided the asList method to convert an Array to a List. In Kotlin, we can certainly use this method to do the conversion.

Next, let’s create a unit test method to see if the method does the job:

val myList = Arrays.asList(givenArray)
assertThat(myList).isEqualTo(expectedList)

If we run the test, it passes. It’s worth mentioning that the Arrays.asList method returns a mutable list.

4. Using the Array.toList Method

Apart from using the standard Java Arrays.asList method to do the conversion, in Kotlin, we can also use the predefined extension function Array.toList() to easily convert an Array object to a List:

val myList = givenArray.toList()
assertThat(myList).isEqualTo(expectedList)

Unlike Java’s Arrays.asList method, Kotlin’s toList() method returns an immutable list. In case we need a mutable list, we can call another extension function: Array.toMutableList.

5. Using the listOf Method

Kotlin’s listOf function is pretty handy when we need to initialize an immutable List object.

The listOf function accepts (vararg elements) as the parameter. In Kotlin, the spread operator (*) can decompose an array instance to a vararg. Next, let’s create a test method using the listOf function to convert the given array to a list:

val myList = listOf(*givenArray)
assertThat(myList).isEqualTo(expectedList)

As a side note, if we need a mutable list, we can call mutableListOf instead.

6. Using the Array.toCollection Method

Kotlin has provided another 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.

7. Conclusion

In this article, we’ve learned how to convert an Array into a List in Kotlin. Moreover, we’ve discussed how to append the given array’s elements to an existing list.

As always, the full source code used in the article can be found over on GitHub.

Authors Bottom

If you have a few years of experience with the Kotlin language and server-side development, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!