Converting Lists to MutableLists and Arrays in Kotlin: A Complete Guide

Table of Contents

Kotlin provides a rich set of utilities for handling collections, making it easier to work with lists and arrays. However, many developers run into issues when trying to convert between different collection types, especially when moving from an immutable List to a MutableList or an Array. In this guide, we’ll cover all possible ways to perform these conversions efficiently and correctly.

Converting a List to a MutableList in Kotlin

By default, a List<T> in Kotlin is immutable, meaning you cannot modify its contents after initialization. If you attempt to change elements using indexing, you’ll encounter the following error:

Kotlin
No 'set' operator method providing array access

This happens because an immutable List does not have a set function. To modify a list, you must first convert it into a MutableList<T>. Let’s explore different ways to achieve this.

1. Using toMutableList() (Recommended)

This is the simplest and most idiomatic way to convert an immutable list into a mutable one:

Kotlin
val list = listOf(1, 2, 3) // Immutable list
val mutableList = list.toMutableList() // Converts to MutableList
mutableList[0] = 10 // Works fine
println(mutableList) // Output: [10, 2, 3]
  • Pros: Simple, clear, and concise.
  • Cons: Creates a new list instead of modifying the original one.

2. Using MutableList() Constructor

If you want to explicitly create a new MutableList, you can use:

Kotlin
val list = listOf("A", "B", "C")
val mutableList = MutableList(list.size) { list[it] }
mutableList[1] = "X" // Works fine
println(mutableList) // Output: [A, X, C]
  • Pros: Useful when you need control over initialization.
  • Cons: Requires a manual copy of elements.

3. Using ArrayList() Constructor

Since ArrayList implements MutableList, you can convert a List to an ArrayList directly:

Kotlin
val list = listOf(4, 5, 6)
val arrayList = ArrayList(list) // Converts to MutableList
arrayList.add(7) // Works fine
println(arrayList) // Output: [4, 5, 6, 7]
  • Pros: Works well with Java interoperability.
  • Cons: Less idiomatic in pure Kotlin.

4. Using toCollection()

This method allows you to add elements from an immutable list into a mutable collection:

Kotlin
val list = listOf(100, 200, 300)
val mutableList = list.toCollection(ArrayList())
mutableList.add(400) // Works fine
println(mutableList) // Output: [100, 200, 300, 400]
  • Pros: Provides flexibility in choosing a mutable collection.
  • Cons: Slightly verbose.

Converting a List to an Array in Kotlin

While lists are more commonly used in Kotlin, there are cases where you need to convert a List<T> into an Array<T>, especially when interacting with Java APIs. Here are different ways to achieve this:

1. Using toTypedArray() (Recommended)

This method is the simplest way to convert a List<T> to an Array<T>:

Kotlin
val list = listOf(1, 2, 3)
val array: Array<Int> = list.toTypedArray() // Converts List to Array
println(array.joinToString()) // Output: 1, 2, 3
  • Pros: Works for all types and is easy to use.
  • Cons: Creates a new array instead of modifying an existing one.

2. Using Array(size) { list[it] } (Manual Copy)

If you need more control over initialization, you can create an array manually:

Kotlin
val list = listOf(10, 20, 30)
val array = Array(list.size) { list[it] }
println(array.joinToString()) // Output: 10, 20, 30
  • Pros: Provides explicit control over array creation.
  • Cons: Slightly verbose compared to toTypedArray().

Which Approach Should You Use?

Conclusion

Kotlin makes it easy to convert between lists and arrays with its powerful utility functions. While toMutableList() and toTypedArray() are the most recommended methods, other techniques can be useful depending on your specific use case. Understanding these conversions ensures smoother interoperability with Java and better flexibility when managing collections in Kotlin.

By following these best practices, you’ll write cleaner, and more efficient Kotlin code.

Skill Up: Software & AI Updates!

Receive our latest insights and updates directly to your inbox

Related Posts

error: Content is protected !!