https://kotlinlang.org logo
Title
o

oday

11/01/2018, 7:56 PM
what’s a kotliny way of using
find
to find the one and only item in a list where I know there is no repetition with, according to its ID?
d

diesieben07

11/01/2018, 7:57 PM
single
?
👍 1
o

oday

11/01/2018, 7:57 PM
ahh
d

diesieben07

11/01/2018, 7:57 PM
That returns the item that matches and throws an exception if there is 0 or more than one that match
o

oday

11/01/2018, 7:57 PM
everytime i think i got it, i miss another thing 😛
d

diesieben07

11/01/2018, 7:57 PM
There is also
singleOrNull
if you want
null
in case no item matches
o

oday

11/01/2018, 7:58 PM
to ensure an item exists, i would use this then
if (leaseCars.singleOrNull { it.id == listingId } != null)
d

diesieben07

11/01/2018, 8:13 PM
that would check that a singular item with that ID exists, yes.
That won't throw an exception if multiple items have the same ID though, it will give you null.