Hi, Is there any guidelines when to use sequences ...
# getting-started
s
Hi, Is there any guidelines when to use sequences vs normal collection operations with some dataset? Is there overhead if sequence is used with small dataset?
s
in general, the main difference is this: in regular collection ops, each operation is run on every element. in sequence/stream operations, all the operations are concatenated into a "pipeline" of sorts, which is then run on every element lazily when a terminal operation is invoked
for example, the
anyMatch((T)->Boolean)
sequence/stream terminal op will run the whole pipeline for every element until a matching element is found; no processing will be done on the rest of the stream. a similar collection op, on the other hand, will run each stage in the pipeline on the whole collection, and then look for something that matches the predicate.
basically: if you have a long series of ops, use sequence/stream and you'll see more significant benefits the larger the dataset. for short series of ops, especially with small datasets, I don't think it really matters.
is a bit too simplistic, but the conclusion is reasonable
s
I wonder how the same benchmark would be now, given that stuff (I hope) has improved since Kotlin 1.2.70. Also, I think it depends a lot on what type of operation we're talking about.