Appropriate collection type for selecting a random element efficiently in Scala?

Define "random". If you mean indexed, then there's no such collection. You can have insertion/deletion in constant time if you give up the "random element" requirement -- ie, you have have non-constant lookup of the element which will be deleted or which will be the point of insertion.

Or you can have constant lookup without constant insertion/deletion.

Define "random". If you mean indexed, then there's no such collection. You can have insertion/deletion in constant time if you give up the "random element" requirement -- ie, you have have non-constant lookup of the element which will be deleted or which will be the point of insertion.

Or you can have constant lookup without constant insertion/deletion. The collection that best approaches that requirement is the Vector, which provides O(log n) for these operations. On the other hand, if you have the element which you'll be looking up or removing, then just pick a HashMap.It's not precisely constant time, but it is a fair approximation.

Just make sure you have a good hash function.

See my edit above – astay13 Nov 28 '11 at 15:56 Until recently I thought that because Vector is an IndexedSeq, it was like an immutable array, but actually it's a tree. This helps explain its performance characteristics. Full Vector description here: scala-lang.org/docu/files/collections-ap..._15.Html – Luigi Plinge Nov 28 '11 at 16:18 I think a HashMap is what I need.

Thanks! – astay13 Nov 29 '11 at 19:30.

As a starting point, take a look at The Scala 2.8 Collections API especially at Performance Characteristics.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions