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

253
Views
Change a value in mutable list in Kotlin

I got this mutablelist:

[Videos(id=4, yt_id=yRPUkDjwr1A, title=test4, likes=0, kat=pranks, ilike=false), Videos(id=3, yt_id=WkyUU9ZDUto, title=test3, likes=0, kat=pranks, ilike=false), Videos(id=2, yt_id=B_X9OQqtduE, title=test2, likes=0, kat=animals, ilike=false), Videos(id=1, yt_id=ywaKlGNiv80, title=test1, likes=0, kat=animals, ilike=false)]

How can I change ilike to true where id is 2

This is what I've tried:

for (i in 0 until vids!!.size) {
    Log.d("lets", vids!!.get(i).title)
        
    if(vids!!.get(i).id == 2){
        vids!!.get(i).ilike = true
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use find function to find the element with id = 2 and change its property:

vids?.find { it.id == 2 }?.iLike = true

Note: it is a good practice to use question mark if the property is nullable and you unsure whether it is null or not.

over 4 years ago · Santiago Trujillo Report

0

If you expect few items (maybe 1 or 2?) to be affected,
you can filter the list and then change iLike of the filtered items:

vids!!.filter { it.id == 2 }.forEach { it.iLike = true }
over 4 years ago · Santiago Trujillo Report

0

Try this, I'm assuming your Videos structure is a data class defined somewhat like so. data class Videos(val id: Int, val yt_id: String, val title: String, val likes: Int, val kat: String, val ilike: Boolean)

list.forEachIndexed { index, video ->
    video.takeIf { it.id == 2}?.let {
        list[index] = it.copy(ilike = 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!