Copy on unmutablelist


Hi everyone,

I have a questions about the copy of unmutable list with Kotlin
For example this code.

val myList = listOf(1,2,3)
val myList2 = maList + 4

In this case, is there a copy of all elements of myList to create myList2 ?
I have the impression that for now listOf() create an arrayList.
If myList contains 100000 element. Is each one copied ?

Is there a way to use LinkedList instead of arrayList ?


This is not directly related to listOf et al, but check out Guava’s ImmutableList.copyOf for smart copying.


Yes, that’s correct.

Yes, just use it as you would with Java:

val myList = listOf(1,2,3)
val myList2 = LinkedList(myList)


It is dammage that listOf is not directly implemented with a linkedList, or something else but an implementation which can do smart copy.
Yes I can write this :

val myList2 = LinkedList(myList)

But myList2 is mutable. and How the creation is done to create this LinkedList ? I guess that all elements are copied.

I guess I will look to vavr or guava.
I don’t really understand why kotlin have a feature of immutable list of set with expensive copy.

And about the coply of objects with the copy() methods, how does that happens ?

For example, I have this class :

data class Person(val name: String, val age : Int, val pets : List<Pet> = listOf())
val me = Person("jerem", 32, listOf(halyna, chalie)
val meOlder = me.copy(age = 33)

What happens in this case, does ‘meOlder’ reference the same list of pets than ‘me’ or the list of pet is duplicated ?


Kotlin does not have immutable lists. They have readonly list which serve a different purpose. It is just a way of declaring that some pice of code will not change the list. In many cases you can still change it, because lists created using listOf are of type ArrayList which is mutable. If you need real immutable lists you should take a look at some libraries which might also give you some improved performance when copying.

Take a look at
In your example meOlder would not create a copy of your pet list. Creating a new copy would not really be possible. You would not want to create a copy of any object as this would lead to many problems due to possible cyclic reference graphs.

Not really. Linked lists can be used to do some smart copy, but that is not what listOf is used most of the time and the speed loss from linked lists when reading is noticeable. If you really need to copy big lists you should look at Guava as @venkatperi pointed out.

You could use

val myList2 = LinkedList(myList).toList()
// or
val myList3: List = LinkedList(myList)

those lists are technically still mutable but as I pointed out above, the result of listOf is mutable as well.


Thanks for you answer. That’s right, ‘read only’ and ‘immutability’ have not exactly the same purpose.
I think I will look to some librairies for that


Incidentally, there is an effort by JB themselves to add immutable collections as an extension to the Kotlin standard library.

See here for the proposal and here for its implementation.