问题
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 replacing it as a var
, or choose a mutable collection as a val
. Which of the mutable collections would best fit the bill?
回答1:
You could consider a DoubleLinkedList, which has a convenient remove()
method to remove the current list cell.
回答2:
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.
来源:https://stackoverflow.com/questions/7760925/preferred-scala-collection-for-progressively-removing-random-items