1

Scala offers so many collections... I am not sure which one to use.

My function generates a collection of a predefined number of elements which are to be iterated one by one by their original order.

All of immutable Vector, Seq and List support iteration, but which one should I use?

  • 2
    FYI http://docs.scala-lang.org/overviews/collections/introduction.html and especially http://docs.scala-lang.org/overviews/collections/concrete-immutable-collection-classes.html – om-nom-nom Feb 26 '13 at 08:54
  • a very detailed answer can be found at http://stackoverflow.com/questions/1722137/scala-2-8-collections-design-tutorial – pagoda_5b Feb 26 '13 at 09:06
  • If your function actually generates the elements it could be worth to return a Stream. This way, only those elements that are actually used are generated which could, depending on your exact use case, reduce some computational overhead. – Martin Thurau Feb 26 '13 at 09:23
  • @MartinThurau I don't think it is quite good tip for a novice. It is quite easy to misuse Streams. – om-nom-nom Feb 26 '13 at 09:31

1 Answers1

0

You're looking for a collection which maintains order. Any collection that extends seq maintains the order in which elements were added.

[scaladocs] Sequences have two principal subtraits, IndexedSeq and LinearSeq, which give different guarantees for performance. An IndexedSeq provides fast random-access of elements and a fast length operation. A LinearSeq provides fast access only to the first element via head, but also has a fast tail operation

List extends LinearSeq, while Vector extends IndexedSeq

Choosing between these will depend on how you wish to access the data, and operations you wish to performs on the data.

Rick-777
  • 9,714
  • 5
  • 34
  • 50
korefn
  • 955
  • 6
  • 17