# Avoiding mutation while working with collection

**URL:** <https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385>\
**Category:** Support\
**Created:** [July 11, 2021, 4:48pm UTC](https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385 "2021-07-11T16:48:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrettJanes](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@BrettJanes](https://discuss.kotlinlang.org/u/BrettJanes)\
**Post date:** [July 11, 2021, 4:48pm UTC](https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385/1 "2021-07-11T16:48:37Z")

</div>

I have some code that needs to build a list over time. Once an element is added, it’s never changed, as it’s recording a history of events.

Wondering what’s the best way to build a collection over time without mutating that’s also performant?

Here’s something I tried that I think mutates only the list reference. Will Kotlin just link existing memory here, or will it actually create copies each time I add? Is there a better way to do this?

```auto
 data class Series<T,R> (val index: T, val value: R)
 typealias IntDouble = Series<Int,Double>
 typealias IntDoubleList = List<Series<Int,Double>>
 
 fun addToCollection(l: IntDoubleList, s: IntDouble) : IntDoubleList {
    return l + s
 }

 fun main() {
     val a = Series(1,3458.20)
     val b = Series(2,3457.40)
     val c = Series(3,3450.91)
     
     var seriesList = listOf(a,b,c)
     seriesList = addToCollection(seriesList, Series(4,3450.55))
 }

```

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 11, 2021, 5:10pm UTC](https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385/2 "2021-07-11T17:10:57Z")

</div>

In this case it will copy the data on each new item.

What do you mean by saying that you need to do this “without mutating”? It sounds like: mutate without mutating.

I don’t say what you need doesn’t make sense at all - there are things like persistent collections, etc. I just think it is important to specify, what is your concern regarding making your collection a normally mutable list.

---

<div class="post-metadata">

**Author:** ![BrettJanes](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@BrettJanes](https://discuss.kotlinlang.org/u/BrettJanes)\
**Post date:** [July 11, 2021, 6:36pm UTC](https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385/3 "2021-07-11T18:36:57Z")

</div>

> [@broot](#):
>
> In this case it will copy the data on each new item.

Ok. So it’s probably better to just use a mutable list here? Does using add() on a mutable list just link another element without any copying?

I’m just getting started coding in a more functional style so want to avoid mutation where it makes sense.

> [@broot](#):
>
> there are things like persistent collections

Thanks for the tip. Just found this:

> **[GitHub - Kotlin/kotlinx.collections.immutable: Immutable persistent...](https://github.com/Kotlin/kotlinx.collections.immutable)**
>
> Immutable persistent collections for Kotlin. Contribute to Kotlin/kotlinx.collections.immutable development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 11, 2021, 7:02pm UTC](https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385/4 "2021-07-11T19:02:05Z")

</div>

If you don’t have a specific need to keep it immutable then I say yes: just make it mutable. Most of the time `add()` does not copy the data.

`mutableListOf()` (actually, `ArrayList`) allocates more space than it needs and then `add()` just puts the item into array at the next free index and increments the size marker. When it gets out of space, it allocates a new, bigger array (usually twice as big) and copies the data. This technique is pretty efficient for a general usage. And I believe it is faster than immutable/persistent collections for such use case as yours.

> [@BrettJanes](#):
>
> Thanks for the tip. Just found this:

Yes, this is exactly the project I meant. If you’re into functional languages and “everything is immutable” paradigm then I guess this is what you need.

---

<div class="post-metadata">

**Author:** ![AlexeySoshin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexeysoshin/32/5976_2.png) [@AlexeySoshin](https://discuss.kotlinlang.org/u/AlexeySoshin)\
**Post date:** [July 14, 2021, 7:28pm UTC](https://discuss.kotlinlang.org/t/avoiding-mutation-while-working-with-collection/22385/5 "2021-07-14T19:28:25Z")

</div>

Sounds like you’re looking for something like a linked list?

```kotlin
    val linkedList = LinkedList<Double>()

    linkedList += 1_3458.20
    linkedList += 2_3457.40
    linkedList += 3_3450.91

```

It is mutable, but the append operation would be more efficient than using a mutableListOf that is backed by an ArrayList.
