what’s a kotliny way of using `find` to find the o...
# getting-started
o
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
single
?
👍 1
o
ahh
d
That returns the item that matches and throws an exception if there is 0 or more than one that match
o
everytime i think i got it, i miss another thing 😛
d
There is also
singleOrNull
if you want
null
in case no item matches
o
to ensure an item exists, i would use this then
if (leaseCars.singleOrNull { it.id == listingId } != null)
d
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.