Preferred Scala collection for progressively removing random items?

烈酒焚心 提交于 2019-12-22 09:11:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!