Got this from filtering a list with multiple filte...
# intellij
b
Got this from filtering a list with multiple filters and I don’t see how it will help performance? Since I need to go through each item the exact same steps (only in a different order) in the two variants. But using a sequence I need to first convert it into a sequence. Why is this a performance boost? The code:
Copy code
features
    .filter { it.segment.contains(value) }
    .filter { inDateRange(it.date) }
    .filter { inVersionRange(it.appVersion) }
    .filter { it.osVersion.contains(Build.VERSION.SDK_INT) }
    .map { it.identifier }
m
First, toSequence for a collection uses the iterator on the existing collection (IIRC), so no real cost here. If you leave it as a list, your code will create 5 new lists, one after each filter. A sequence processes each element through all the steps (or at least until it gets filtered out), so it only creates one list. That's where the gain can come from.
b
Thank you for that clarification! 🙏
p
Yet if your features list does not countain a lot of elements there is no performance to gain
I use sequences only if there is some way of early abort
like .sequence-filter-filter-filter-first()
b
Yeah, I've got a shortlist of 20 items and just a bunch of filters. And even if I use a sequence, I will perform the same amount of filtering calls. But now I can make an informed decision if I want to make the switch or not. So I'll stick with the list for now.
👍 1