Muhammed Salih Karakasli

Software Engineer | Mobile Development | Personal Notes

Collections vs Sequences in KOTLIN, which one you should prefer?

We mostly get used to use collections in daily programming with KOTLIN, but there is another brother of Collections in KOTLIN is Sequences.

Sequences are pretty similar when you are writing, however there are huge differences when executing.

Let’s check main differences;

  1. Sequences are executed lazily when possible, computing happens only when the result of the whole processing chain is requested.
  2. Sequences perform all the processing steps one-by-one for every single element, instead of completing each step for the whole collection and then proceeds to the next step.

So, the sequences let you avoid building results of intermediate steps, therefore improving the performance of the whole collection processing chain.


But this is not mean to always use Sequences instead of KOTLIN, as always it totally depends on your use case.

In a short summary, sequences are most performer if you works on large data by filtering especially, on the other hand eager executing may be better for small sets of data with simple computations.