1. Introduction

In Kotlin, we use collections to store and manipulate a group of related objects. Specifically, the Collection interface serves as the root of the collection hierarchy, and several classes implement it, such as List and Set. However, there may be instances where we need to convert a collection into an ArrayList.

In this tutorial, we’ll explore some ways to convert a Collection into an ArrayList in Kotlin.

2. Using a for Loop and the add() Method

A straightforward way to convert a collection into an ArrayList is by using a for() loop and the add() method:

@Test
fun `converts a Collection to ArrayList using the for loop and add method`() {
    val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
    val arrayList = ArrayList<String>()
    for (element in collection) {
        arrayList.add(element)
    }

    assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
}

In the code above, we create a collection of strings using the listOf() method. Next, we iterate through all the elements of our collection and use the add() method to append each one to the ArrayList. Finally, we assert that the new ArrayList contains all the elements from our collection.

3. Using the ArrayList Constructor

We can also convert a collection into an ArrayList by using its constructor. The constructor takes a collection as an argument and returns an ArrayList with the same elements as the collection:

@Test
fun `converts a Collection to ArrayList using the ArrayList constructor`() {
    val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
    val arrayList = ArrayList(collection)

    assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
}

In this unit test, we pass our collection of strings to the ArrayList() constructor to create an ArrayList with the same elements as the original list. Finally, we assert that the array list contains the correct elements.

4. Using the toCollection() Method

Another way to convert a collection into an ArrayList involves the use of the toCollection() method. This method accepts an empty ArrayList as an argument, and it will append all elements from the original collection to the ArrayList parameter:

@Test
fun `converts a Collection to ArrayList using the toCollection method`() {
    val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
    val arrayList = collection.toCollection(ArrayList())

    assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
}

In this code, we use the listOf() method to create a collection of strings. Subsequently, we call the toCollection() method on this collection of strings and pass in an empty ArrayList as an argument. This will then return an ArrayList with the same elements as the collection.

5. Using the ArrayList.addAll() Method

Additionally, we can use addAll() method on the ArrayList class to add all elements of a collection to an empty ArrayList:

@Test
fun `converts a Collection to ArrayList using the addAll method`() {
    val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
    val arrayList = ArrayList<String>()
    arrayList.addAll(collection)

    assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
}

In this code snippet, we create an empty ArrayList of strings and add all the elements of a collection of strings using the addAll() method.

6. Using the mapTo() Method

To convert a collection into an ArrayList, we can use the mapTo() method, which takes two arguments. First, we need to provide the destination ArrayList that we want to create. Secondly, we need to pass a lambda function that maps each element in the original collection to the corresponding element in the destination ArrayList:

@Test
fun `converts a Collection to ArrayList using the mapTo method`() {
    val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
    val arrayList = ArrayList<String>()
    collection.mapTo(arrayList) { it }

    assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
}

In this code snippet, we use the identity function as the mapping function. This lambda function returns each element unchanged and is, therefore, a simple and efficient way to map the elements from the original collection to the destination ArrayList.

Finally, we use the assertEquals() method to verify that the converted ArrayList contains the expected elements.

7. Conclusion

In this article, we’ve explored various ways to convert a collection into an ArrayList in Kotlin. While some approaches are more efficient and concise than others, it’s important to note that each approach is valid. We should take time to consider the context and requirements of our applications when deciding which approach to adopt.