Sebastien Leclerc Lavallee
05/01/2020, 9:18 PMval listOfItems: List<Item> = listOf<Item>
fun getItemWithId(id: String): Item {
return listOfItems.filter { it.id == id }.first()
}
A map of Item per String
val mapOfItems: Map<String,Item>
Or any other solution I didn’t think of?
Thanks 🙂mathew murphy
05/01/2020, 9:20 PMmarstran
05/01/2020, 9:34 PMfind
function, instead of doing filter + first.marstran
05/01/2020, 9:36 PMfind
everytime depends on how many times you need to use it. Creating a map has a performance cost up front, but when you have the map it's fast. Using find
will make you loop through the list every time.Thomas
05/01/2020, 10:25 PMreturn listOfItems.first { <http://it.id|it.id> == id }
Sebastien Leclerc Lavallee
05/02/2020, 2:43 AMThomas
05/03/2020, 1:48 PM