根据the docs,自{2.1}版本起,scala.collection.mutable.LinkedList
已弃用。不幸的是,我没有找到任何替代它。我需要一个有序的集合,它可以在固定的时间内从任何索引中删除一个项目。
我应该使用什么?
答案 0 :(得分:5)
使用MutableList及其迭代器的remove
方法。它们提供O(1)去除。
答案 1 :(得分:4)
From what I understand of your problem you want to iterate through collection and change it on the fly. That is not possible with collection constructs other than (now deprecated) scala.collection.mutable.LinkedList
or scala.collection.mutable.DoubleLinkedList
. This kind of operation doesn't really follow the Scala collections philosophy hence LinkedList
and DoubleLinkedList
are deprecated now.
However nothing stops you from using classic Java's java.util.LinkedList
and relevant iterator in your Scala code.
Unless you want to review your design and follow Scala's way using constructs like: map
, filter
, for
, fold
, reduce
, etc. For example, using filter
function you can create a new list with only relevant items.