Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Modifying a SnapshotStateList throws ConcurrentModificationException

The documentation of SnapshotStateList states that it is similar to a regular mutable list. I have a use case where I need to modify all the elements in the list (set case). This does not change the size of the list, but I'm running into ConcurrentModificationException.

I have created a very simplified version of my usecase here. The following kotlin list works fine:

val myList2 = mutableListOf("a", "b", "c")
myList2.forEachIndexed { index, _ ->
    // Modify item at index
    myList2[index] = "x"
}

But I get a concurrent modification exception here:

val myList = mutableStateListOf("a", "b", "c")
myList.forEachIndexed { index, _ ->
    // Modify item at index but I get an exception
    myList[index] = "x"
}

How can I modify all elements of mutableStateList() in place without getting the concurrent modification exception?

Edit:

I can create a copy of the mutableStateList to iterate over which works fine but since I'm not changing the size of the list, is it possible to do it in place?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Some possible workarounds are to use replaceAll to transform the list in-place (as long as you don't need the index), or just use an old-fashioned loop over indices if you do

val listA = mutableListOf("A","B","C")

// this works
listA.forEachIndexed { i, s ->
    listA[i] = s.lowercase()
}

val listB = mutableStateListOf("A","B","C")

// this fails - as you noted
listB.forEachIndexed { i, s ->
    listB[i] = s.lowercase()
}

// this works, as long as you don't need the index
listB.replaceAll { s -> s.lowercase() }

// this also works, and lets you have the index
for(i in listB.indices) {
    listB[i] = listB[i].lowercase()
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!