Dina Bogdan
Dina Bogdan

Reputation: 4688

What are the differences between LazyList and List in Scala?

What are the differences between the following collections types in Scala: List and LazyList types?

Upvotes: 8

Views: 4261

Answers (2)

Mario Galic
Mario Galic

Reputation: 48400

The question

What are the differences between LazyList and List?

can be rephrased as the question

What are the differences between Stream and List?

because by Scala 2.13 release notes

immutable.LazyList replaces immutable.Stream. Stream had different laziness behavior and is now deprecated. (#7558, #7000)

and the answer to the rephrased question is provided by what is the difference between Scala Stream vs Scala List vs Scala Sequence.

Performance judgements are best addressed by measurements within particular scenarios.

Upvotes: 4

Dina Bogdan
Dina Bogdan

Reputation: 4688

LazyList is a new type introduced in Scala Standard Library 2.13.1.

  • The type it's an immutable one and it's placed into scala.collection.immutable package. The major difference between the common List type is the fact that the elements of the LazyList are computed lazily, so only those elements that are requested are computed. By this means, a lazy list can have an infinite number of elements.
  • In terms of performance, the two types (LazyList and List) are comparable.
  • A LazyList is constructed with an operator having a similar-looking to the one specific to the List type (::), #::.
  • Being lazy, a LazyList can't produce a StackOverFlowError in a recursive loop, as an old List could do.

Upvotes: 6

Related Questions