do I place it before every filter operation on my ...
# getting-started
o
do I place it before every filter operation on my results if they are like in the 100's?
p
There is no guaranteed performance improvement. It just turns it into a lazy stream-like collection.
so this will apply
map
to all items:
Copy code
items.map(..).first()
but this will apply
map
to only the first item:
Copy code
items.asSequence().map(..).first()
o
yea I get how it works in code
but as a concept I mean, I don’t think it has to do with performance because eventually in an example of 100000 results, it’ll get you want you want first but that doesn’t mean that the loop will finish earlier
p
So make it lazy only where it makes sense. There is no magic number of items where it starts to be an improvement.
o
that’s the example im basing my question on
p
ofc it will finish earlier because it will map just one item instead of 100000
o
but I saw Hadi try it in that course of his 😛
the loop hangs but you do get output early
p
I am not sure if I follow. In the sequence case it will literally map just the first item. It will not hang, it will return almost immediately. In the list case, it will first apply the map on all 100 000 items and then it will give you the first one, effectively wasting time doing unnecessary mappings.
Also, in the second case it will allocate almost no memory while in the first case it will need to allocate memory for the whole intermediate collection which could potentially not even fit in your RAM.
m
Let's say you have this:
Copy code
collectionWithAMillionElements
    .map { doSomethingExpensive(it) }
    .forEach(::println)
If this is on a list, then it will do
doSomethingExpensive
on every element before it starts printing. If it's on a sequence, then it will call
doSomethingExpensive
on the first element, print it, and the continue with the next element.
o
great
but on that million elements list, will the “looping” to map be done earlier?
p
yes on a list, the loop will happen immediately.. on a sequence it will be only triggered by a terminal operation
o
both of them are traversing the entire thing, but one is not waiting until the end to produce the result, and one waits until the end to produce the result, but they both have to go to the end
p
not in my case
o
oh you had
first()
of course it would end early
p
yep, I was trying to illustrate the difference
o
ahh
then for sure both go to the very end of the list if there is no first or single or anything like take(x)
well, take only shrinks the result, but it will still go to the end naturally
i thought there was magic or something
p
yeah, pretty much
o
oki, thanks
p
so then the difference is only in memory allocation, sequence only allocates memory per one item
list operations need to create the whole collections at each step
o
that’s a big advantage if its a large result set
p
yup