Generally speaking... running `listOfIds.contains...
# getting-started
c
Generally speaking... running
listOfIds.contains(this.id)
will run slower than
setOfIds.contains(this.id)
right?
c
Searching a list is O(n), whereas a set lookup is O(1).
e
on the other hand, a set takes a bit more memory and may take significantly more time to construct, especially if you have very deep structures of `data class`es and other containers (as
hashCode()
isn't by default (and can't generally be) cached)
if you already have a list, and you are only checking one element for membership once, then there is zero purpose to converting the list to a set first
if any of those preconditions change, then a set may be a better idea
c
Cool. That's roughly where my mind was at as well, but wanted a sanity check