简体   繁体   中英

Seq for fast random access and fast growth in Scala

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

  • Sequentially ordered, so I can access items by position (a Seq)
  • Need to insert items frequently, so the collection must be able to grow without too much penalty
  • Random access, frequently need to remove and insert items at arbitrary indexes in the collection

Currently I seem to be getting good performance with the mutable ArrayBuffer, but is there anything better? Is there an immutable alternative that would do as well? Thanks in advance.

Mutable: ArrayBuffer
Immutable: Vector

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

Vector . IndSeq from scalaz should be even better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM