Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda