Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda