开发者

Preferred Scala collection for progressively removing random items?

开发者 https://www.devze.com 2023-04-13 01:48 出处:网络
I have an algoritm which takes many iterations, each of which scores items in a collection and removes the one with the highest score.

I have an algoritm which takes many iterations, each of which scores items in a collection and removes the one with the highest score.

I could populate a Vector with the initial population, continually开发者_JAVA技巧 replacing it as a var, or choose a mutable collection as a val. Which of the mutable collections would best fit the bill?


You could consider a DoubleLinkedList, which has a convenient remove() method to remove the current list cell.


I think a Map (or its close relative, the Set) might do well. It doesn't have indexed access, but that doesn't seem to be what you want. If you go for a TreeMap, you'll even get an ordered collection.

However, might I point out that your algorithm seems to call for a Heap? A heap is optimized for repeatedly finding/removing the maximum element (or minimum, if you invert the the comparison building the heap). Scala doesn't have a ready made heap, but a heap is easily implemented with an array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号