Seq for fast random access and fast growth in Scala

What would be the best Scala collection (in version 2.8+), mutable or immutable, for the following scenario:

  • Sequential ordering, so I can access elements by position (Seq)
  • You need to insert elements often, so the collection should be able to grow without much penalty
  • Random access, it is often necessary to delete and insert elements into arbitrary indexes in the collection

Currently, I seem to be getting good performance with a mutable ArrayBuffer, but is there anything better? Is there an indisputable alternative that will do? Thanks in advance.

+4
source share
3 answers

Mutable: ArrayBuffer
Immutability: Vector

+4
source

If you insert elements at random positions more than the log (N) / N time that you are accessing them, then you should probably use immutable.TreeSet , since all operations are O (log (N)). If you basically do access or add to the (far) end, ArrayBuffer and Vector work well.

+4
source

Vector . IndSeq from scalaz should be even better.

+2
source

All Articles