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

227
Visualizações
kotlin arraylist need to update only one item

I am trying to update only one item but the below code is updating all

logValue("changes before : $availabilityMasterList")

                availabilityMasterList.filter { it.days.lowercase() == day.lowercase() }.forEach {
                    it.shifts.add(Shift(shiftname, "", ""))
                }

                logValue("changes broadcast : $availabilityMasterList")

log output before and after

changes before : [MasterAvailableListData(days=Monday, drsAvailabilityDate=, id=0, shifts=[], edit=false), MasterAvailableListData(days=Tuesday, drsAvailabilityDate=, id=1, shifts=[], edit=false), MasterAvailableListData(days=Wednesday, drsAvailabilityDate=, id=2, shifts=[], edit=false), MasterAvailableListData(days=Thursday, drsAvailabilityDate=, id=3, shifts=[], edit=false), MasterAvailableListData(days=Friday, drsAvailabilityDate=, id=4, shifts=[], edit=false), MasterAvailableListData(days=Saturday, drsAvailabilityDate=, id=5, shifts=[], edit=false), MasterAvailableListData(days=Sunday, drsAvailabilityDate=, id=6, shifts=[], edit=false)]

---------- below output after above query --------------------

2021-11-23 14:50:04.428 30467-30467/com.ohc.admin D/OhcDocHosp: ---------- changes broadcast : [MasterAvailableListData(days=Monday, drsAvailabilityDate=, id=0, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Tuesday, drsAvailabilityDate=, id=1, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Wednesday, drsAvailabilityDate=, id=2, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Thursday, drsAvailabilityDate=, id=3, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Friday, drsAvailabilityDate=, id=4, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Saturday, drsAvailabilityDate=, id=5, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false), MasterAvailableListData(days=Sunday, drsAvailabilityDate=, id=6, shifts=[Shift(shiftname=Afternoon, startTime=, endTime=)], edit=false)]

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I highly suspect that each of the MasterAvailableListData items have the exact same reference to the same empty shifts list. So if you add a shift to a shifts list on any of the items it looks like you added it to all, because it is in fact the same list. Make sure you create the MasterAvailableListData items each with their own initial empty shifts list

over 4 years ago · Santiago Trujillo Relatório

0

It weirds that it returns only one element and that the whole list is updated...

If you need to update one item only, use the firstOrNull

Do something like that


 availabilityMasterList.firstOrNull { it.days.equals(day, true) }.?apply {
     shifts.add(Shift(shiftname, "", ""))
  }

firstOrNull will returns that matched value if any, otherwise null

You don't need to iterate on the result, because you will obtain the item and not a list.

?.apply

It will prevent from the null pointer and update the item only if the item exists in the list. apply will also return the element, and you don't need to use the it or the this to reference the object.

BTW, if you need a case-insensitive comparison, you can use the equals function with the ignoreCase param. (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/equals.html)

"example".equals("EXAMPLE", ignoreCase = true)

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