If I have to filter a list, is it always better (p...
# announcements
a
If I have to filter a list, is it always better (performance-wise) to convert it to a flow first or did I misunderstand something?
s
No, unless you really need a Flow, don't convert it to a Flow. Just keep it as a list and call
filter
on it. For large lists and if you call many operators on the list, you may want to convert it to a
Sequence
.
Also, avoid optimizing (early) if you don't see or experience a performance issue. 😀
💯 3
a
Thanks, but flows/sequences would make sense if I had a larger list of data entities and want to map it to a list of domain entities, and might also filter afterwards, right? I will look up the difference between flow and sequence.
s
Flow is a (cold) stream of data. You'd need a Coroutine to collect the data emitted by a Flow. A Sequence is a 'lazy' view on a collection (list). No need for Coroutines.
âž• 1
a
Understood, great explanation, thank you!
m
If it’s not a huge list, just call
list.filter {}
and be fine. I support what Anton said: don’t optimize prematurely, only if you experience a slow execution.