Issue
There are two lists:
val listA:List<Amodel>
val listB:List<Int>
data class Amodel(val id:Int,var isUsed:Boolean=false)
Need to update listA isUsed =true based on the int id of the listB
listB.foreach(){
//here i'm updating but it's taking too much time w.r.t time efficiency.
}
any GOOD solution.
Note list size is 500k+ for simulation
Solution
This might be slightly faster when the lists are huge:
val lookup = listB.associateWith { true }
listA.forEach { it.isUsed = lookup[it.id] ?: false }
Possibly this is even faster, I'm not sure. Because it then only sets isUsed
in the case it needs to be true:
val lookup = listB.associateWith { true }
listA.forEach { lookup[it.id]?.run { it.isUsed = true } }
Answered By - Ivo Beckers
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)