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

221
Views
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 answers
Answer question

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 Report

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 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!