Scala 2.11 LinkedList is deprecated, what should I use?

天涯浪子 提交于 2019-12-09 08:08:51

问题


According to the docs, scala.collection.mutable.LinkedList is deprecated as of the 2.11 version. Unfortunately I have found nothing to replace it with. I need an ordered collection that can remove an item from any index in constant time.

What should I use?


回答1:


Use MutableList and its iterator's remove method. They provide O(1) removal.

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




回答2:


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.



来源:https://stackoverflow.com/questions/27557427/scala-2-11-linkedlist-is-deprecated-what-should-i-use

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