66

I have a list:

val someList = listOf(1, 20, 10, 55, 30, 22, 11, 0, 99)

And I want to iterate it while modifying some of the values. I know I can do it with map but that makes a copy of the list.

val copyOfList = someList.map { if (it <= 20) it + 20 else it }

How do I do this without a copy?

Note: this question is intentionally written and answered by the author (Self-Answered Questions), so that the idiomatic answers to commonly asked Kotlin topics are present in SO. Also to clarify some really old answers written for alphas of Kotlin that are not accurate for current-day Kotlin.

Community
  • 1
  • 1
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227

5 Answers5

103

First, not all copying of a list is bad. Sometimes a copy can take advantage of CPU cache and be extremely fast, it depends on the list, size, and other factors.

Second, for modifying a list "in-place" you need to use a type of list that is mutable. In your sample you use listOf which returns the List<T> interface, and that is read-only. You need to directly reference the class of a mutable list (i.e. ArrayList), or it is idiomatic Kotlin to use the helper functions arrayListOf or linkedListOf to create a MutableList<T> reference. Once you have that, you can iterate the list using the listIterator() which has a mutation method set().

// create a mutable list
val someList = arrayListOf(1, 20, 10, 55, 30, 22, 11, 0, 99)

// iterate it using a mutable iterator and modify values 
val iterate = someList.listIterator()
while (iterate.hasNext()) {
    val oldValue = iterate.next()
    if (oldValue <= 20) iterate.set(oldValue + 20)
}

This will change the values in the list as iteration occurs and is efficient for all list types. To make this easier, create helpful extension functions that you can re-use (see below).

Mutating using a simple extension function:

You can write extension functions for Kotlin that do an in place mutable iteration for any MutableList implementation. These inline functions will perform as fast as any custom use of the iterator and is inlined for performance. Perfect for Android or anywhere.

Here is a mapInPlace extension function (which keeps the naming typical for these type of functions such as map and mapTo):

inline fun <T> MutableList<T>.mapInPlace(mutator: (T)->T) {
    val iterate = this.listIterator()
    while (iterate.hasNext()) {
        val oldValue = iterate.next()
        val newValue = mutator(oldValue)
        if (newValue !== oldValue) {
            iterate.set(newValue)
        }
    }
}

Example calling any variation of this extension function:

val someList = arrayListOf(1, 20, 10, 55, 30, 22, 11, 0, 99)
someList.mapInPlace { if (it <= 20) it + 20 else it }

This is not generalized for all Collection<T>, because most iterators only have a remove() method, not set().

Extension functions for Arrays

You can handle generic arrays with a similar method:

inline fun <T> Array<T>.mapInPlace(mutator: (T)->T) {
    this.forEachIndexed { idx, value ->
        mutator(value).let { newValue ->
            if (newValue !== value) this[idx] = mutator(value)
        }
    }
}

And for each of the primitive arrays, use a variation of:

inline fun BooleanArray.mapInPlace(mutator: (Boolean)->Boolean) {
    this.forEachIndexed { idx, value ->
        mutator(value).let { newValue ->
            if (newValue !== value) this[idx] = mutator(value)
        }
    }
}

About the Optimization using only Reference Equality

The extension functions above optimize a little by not setting the value if it has not changed to a different instance, checking that using === or !== is Referential Equality. It isn't worth checking equals() or hashCode() because calling those has an unknown cost, and really the referential equality catches any intent to change the value.

Unit Tests for Extension Functions

Here are unit test cases showing the functions working, and also a small comparison to the stdlib function map() that makes a copy:

class MapInPlaceTests {
    @Test fun testMutationIterationOfList() {
        val unhappy = setOf("Sad", "Angry")
        val startingList = listOf("Happy", "Sad", "Angry", "Love")
        val expectedResults = listOf("Happy", "Love", "Love", "Love")

        // modify existing list with custom extension function
        val mutableList = startingList.toArrayList()
        mutableList.mapInPlace { if (it in unhappy) "Love" else it }
        assertEquals(expectedResults, mutableList)
    }

    @Test fun testMutationIterationOfArrays() {
        val otherArray = arrayOf(true, false, false, false, true)
        otherArray.mapInPlace { true }
        assertEquals(arrayOf(true, true, true, true, true).toList(), otherArray.toList())
    }

    @Test fun testMutationIterationOfPrimitiveArrays() {
        val primArray = booleanArrayOf(true, false, false, false, true)
        primArray.mapInPlace { true }
        assertEquals(booleanArrayOf(true, true, true, true, true).toList(), primArray.toList())
    }

    @Test fun testMutationIterationOfListWithPrimitives() {
        val otherList = arrayListOf(true, false, false, false, true)
        otherList.mapInPlace { true }
        assertEquals(listOf(true, true, true, true, true), otherList)
    }
}
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • 1
    Should not you use `MutableListIterator`? I cannot find `set` in iterator – Amr Eladawy Jun 03 '17 at 13:55
  • For primitive arrays you should use Structural equality `!=` in place of Referential equality `!==` – wrozwad Feb 13 '18 at 12:35
  • 1
    @AmrElAdawy In the examples I am using the `listIterator()` returned from mutable structures which therefore returns a `MutableListIterator`, so the code as above works. If you are typing your iterator or using a non-mutable list to get the iterator, then, of course, you will have `ListIterator` instead. The code above infers the correct types. – Jayson Minard Feb 19 '18 at 16:30
  • @sosite the referential equality is intentional, it is asking if the mutator gave back a new object, not if it modified the contents or gave back an equivalent object. It is just a quick performance check and if these lists were observable to avoid firing off events. It would be slow to do full equality checks on the contents and this wasn't intended to do that anyway. – Jayson Minard Feb 19 '18 at 16:32
  • @JaysonMinard But identity equality for arguments of primitive types is deprecated – wrozwad Feb 20 '18 at 11:22
  • Ah, yes, that did happen. Makes sense, depending on what is in the list. – Jayson Minard Feb 20 '18 at 21:37
2

Here's what I came up with, which is a similar approach to Jayson:

inline fun <T> MutableList<T>.mutate(transform: (T) -> T): MutableList<T> {
    return mutateIndexed { _, t -> transform(t) }
}

inline fun <T> MutableList<T>.mutateIndexed(transform: (Int, T) -> T): MutableList<T> {
    val iterator = listIterator()
    var i = 0
    while (iterator.hasNext()) {
        iterator.set(transform(i++, iterator.next()))
    }
    return this
}
EntangledLoops
  • 1,951
  • 1
  • 23
  • 36
0

Here is a custom solution, with an example :

val sorted: MutableList<Pair<Double, T>> = ...
val groups: mutableListOf<List<T>>() = ...

sorted.forEachMutable { it ->
    if (size + it.first <= maxSize) {
        size += it.first
        group += it.second
        this.remove() // Removing from iterable !
    }
}

Here is code for "forEachMutable" :

fun <T> MutableIterable<T>.forEachMutable(block: Removable<T>.(T) -> Unit): Unit {
    val iterator: MutableIterator<T> = iterator()
    val removable = Removable(iterator, block)
    while (iterator.hasNext()) {
        val item = iterator.next()
        removable.action(item)
    }
}

class Removable<T>(
    private val iterator: MutableIterator<T>,
    private val block: Removable<T>.(T) -> Unit) {

    fun remove() =
        iterator.remove()

    fun action(item: T) {
        block(item)
    }
}

Maxime

-2

Without having to write any new extension methods - yes, the functional paradigms are awesome, but they do generally imply immutability. If you're mutating, you might consider making that implicit by going old school:

    val someList = mutableListOf(1, 20, 10, 55, 30, 22, 11, 0, 99)

    for(i in someList.indices) {
        val value = someList[i]
        someList[i] = if (value <= 20) value + 20 else value
    }
Byron Katz
  • 492
  • 5
  • 10
-3

You can use list.forEach { item -> item.modify() }

This will modify each item in list while iterating.