Skip to content

Sequences in kotlin

Devrath edited this page Feb 28, 2024 · 6 revisions

Sequences

Background

  • There are many ways to handle a group of items.
  • Collections and Sequences provide many utilities.
  • Eagerly with the collections and Lazily with sequences.

Difference b/w Eagerly and Lazy

  • Depends on when the transformation is performed on the collection

Collection use Eagerly

  • On each operation, A new collection is formed and modified based on the operator's nature.
  • Operations on the collections are implemented using inline functions.

Sequences use Eagerly

  • Sequence is created based on the iterator of the original collection.
  • So we need to add asSequence()
  • The operations are added to the list of operations to be performed but the operations are not performed immediately
  • Instead, the Terminal operator is applied first if it contains it, and thus map is not applied if it contains it.
  • Also new copy is not created on each operator invocation.
  • Each operation is not an inline function.

Clone this wiki locally