Scala 2.11 LinkedList已弃用,我应该使用什么?

时间:2014-12-18 23:20:22

标签: scala collections linked-list

根据the docs,自{2.1}版本起,scala.collection.mutable.LinkedList已弃用。不幸的是,我没有找到任何替代它。我需要一个有序的集合,它可以在固定的时间内从任何索引中删除一个项目。

我应该使用什么?

2 个答案:

答案 0 :(得分:5)

使用MutableList及其迭代器的remove方法。它们提供O(1)去除。

http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#linked_lists

答案 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.