Clone a list in Kotlin
This article explores different ways to clone a list in Kotlin.
1. Using Copy Constructor
A simple and efficient solution is to use a copy constructor to clone a list, which creates a new object as a copy of an existing object.
|
1 2 3 4 5 6 7 |
fun main() { val original: List<Int> = listOf(1, 2, 3, 4, 5); val copy: List<Int> = ArrayList(original) println(copy) // [1, 2, 3, 4, 5] } |
2. Using addAll() function
You can also use the addAll() function that appends all the given list elements at the end of a new list.
|
1 2 3 4 5 6 7 8 |
fun main() { val original: List<Int> = listOf(1, 2, 3, 4, 5) val copy: MutableList<Int> = ArrayList() copy.addAll(original) println(copy) // [1, 2, 3, 4, 5] } |
This can be shortened using the apply() function:
|
1 2 3 4 5 6 7 |
fun main() { val original: List<Int> = listOf(1, 2, 3, 4, 5); val copy: List<Int> = mutableListOf<Int>().apply { addAll(original) } println(copy) // [1, 2, 3, 4, 5] } |
3. Using toCollection() function
Another solution is to use the toCollection() function, which appends all elements to the given destination collection.
|
1 2 3 4 5 6 7 |
fun main() { val original: List<Int> = listOf(1, 2, 3, 4, 5); val copy: List<Int> = original.toCollection(mutableListOf()) println(copy) // [1, 2, 3, 4, 5] } |
4. Using mutableListOf() function
You can also use the toList() or toMutableList() function to create a copy of a collection. Although, there is no official documentation that guarantees that this function returns a new copy of the list.
|
1 2 3 4 5 6 7 8 9 10 |
fun main() { val original: List<Int> = listOf(1, 2, 3, 4, 5); println(original) // [1, 2, 3, 4, 5] val copy: MutableList<Int> = original.toMutableList() copy.removeLast() println(copy) // [1, 2, 3, 4] } |
5. Implement the Cloneable Interface
If you’re dealing with a list of objects, you can have the class implement the Cloneable interface and override its clone() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
data class Person(var name: String, var age: Int) : Cloneable { public override fun clone(): Person { return Person(name, age) } } fun main() { val original = listOf(Person("p1", 25), Person("p2", 20)) println(original) // [Person(name=p1, age=25), Person(name=p2, age=20)] val copy = original.map(Person::clone).toList(); copy[0].age = 22 println(copy) // [Person(name=p1, age=22), Person(name=p2, age=20)] } |
6. Using Data class
If your list is storing objects of a data class, you can use the copy() function derived by the compiler.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
data class Person(var name: String, var age: Int) fun main() { val original = listOf(Person("p1", 25), Person("p2", 20)) println(original) // [Person(name=p1, age=25), Person(name=p2, age=20)] val copy = original.map { it.copy() } copy[0].age = 22 println(copy) // [Person(name=p1, age=22), Person(name=p2, age=20)] } |
That’s all about cloning a list in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)