What are the differences between the following collections types in Scala: List and LazyList types?
What are the differences between the following collections types in Scala: List and LazyList types?
LazyList is a new type introduced in Scala Standard Library 2.13.1.
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.LazyList and List) are comparable.LazyList is constructed with an operator having a similar-looking to the one specific to the List type (::), #::.LazyList can't produce a StackOverFlowError in a recursive loop, as an old List could do.The question
What are the differences between
LazyListandList?
can be rephrased as the question
What are the differences between
StreamandList?
because by Scala 2.13 release notes
immutable.LazyListreplacesimmutable.Stream.Streamhad 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.