Skip to content
DeveloperMemos

How to Clone a List in Kotlin

Kotlin, Cloning a List, Shallow Copy2 min read

Kotlin provides several ways to clone a list. Cloning a list means creating a new list with the same elements as the original list. The new list should be independent of the original list, so any changes made to the new list should not affect the original list, and vice versa. In this article, we will explore different methods to clone a list in Kotlin.

1. Using the toList() method

The toList() method is a simple and straightforward way to create a new list from an existing list. It creates a shallow copy of the list, meaning it only copies the references to the elements in the list, not the elements themselves.

1val originalList = listOf(1, 2, 3)
2val clonedList = originalList.toList()

2. Using the copyOf() method

The copyOf() method is another convenient way to clone a list in Kotlin. It creates a new list with the same elements as the original list. Like toList(), copyOf() also creates a shallow copy, preserving the references to the elements.

1val originalList = listOf(1, 2, 3)
2val clonedList = originalList.copyOf()

3. Using the map() method

The map() method in Kotlin is primarily used for transforming elements in a collection. However, it can also be leveraged to clone a list. By applying the identity function, which returns its input, we can create a new list with the same elements as the original list.

1val originalList = listOf(1, 2, 3)
2val clonedList = originalList.map { it }

4. Using the toMutableList() method

If you need a mutable clone of a list, you can use the toMutableList() method. It converts an existing list into a new mutable list. Similar to the previous methods, toMutableList() creates a shallow copy of the list, preserving the references.

1val originalList = listOf(1, 2, 3)
2val clonedList = originalList.toMutableList()

5. Using the ArrayList constructor

Another way to clone a list and obtain a mutable copy is by utilizing the ArrayList constructor. It takes an existing list as a parameter and creates a new mutable list with the same elements.

1val originalList = listOf(1, 2, 3)
2val clonedList = ArrayList(originalList)
ExpressVPN

Protect your online privacy and security with ExpressVPN, a reliable and fast VPN service.
Sign up now to get 3 months free!

We earn a commission if you make a purchase, at no additional cost to you.

Additional Considerations

When cloning a list, it's important to keep in mind that the methods mentioned above create shallow copies. This means that they copy the references to the elements in the list, not the elements themselves. If the elements are objects or other collections, changes made to those objects or collections will reflect in both the original list and the cloned list. To achieve a deep copy, where the elements themselves are copied, you would need to manually create a new list and copy each element.

In conclusion, Kotlin offers several convenient methods to clone lists, including toList(), copyOf(), map(), toMutableList(), and the ArrayList constructor. It's essential to understand the distinction between shallow and deep copies when working with cloned lists, as it determines whether changes to the elements will propagate to both lists or remain independent.