Scala DoubleLinkedList replacement

徘徊边缘 提交于 2019-12-11 00:28:57

问题


DoubleLinkedList is deprecated since Scala 2.11.0 (http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.DoubleLinkedList$). Why is this? There doesn't seem to be a clear replacement for it. Is there any plans for a successor?


回答1:


"Idiosyncratic and dangerous" API, as it is described in the commit message and deprecation, sums it up.

The general direction has been to reduce the size of the standard library, and of collections in particular, so it's likely that a replacement would arrive (if ever) only as part of a re-imagined and re-engineered replacement collections library. (There are efforts underway.)

The last comments on this issue are of the order, "How is this even supposed to work?" It's surprisingly difficult to reconcile many of the standard API of the 2.8 collections with mutability, which is one reason the experiment to unify mutable and immutable collections behind common interfaces has lived up to its hype.




回答2:


Answering what is a replacement for DoubleLinkedList, alas, as of 2.12.6, there is no non-deprecated collection type that offers constant time addition/removal from both ends. Vector comes close, but there's no way to remove elements from it, only replace (using update). I had a similar requirement, and ended up using java.util.LinkedList. And yes, this is one use case where Java offers a feature not present in Scala.

Sample code:

import scala.collection.JavaConverters._
val xs = new java.util.LinkedList[T]().asScala
// remove from beginning, O(1)
xs.remove(0)
// remove from end, O(1)
xs.remove(xs.size - 1)
// append, O(1)
xs += e
// prepend, O(1)
e +=: xs


来源:https://stackoverflow.com/questions/27636154/scala-doublelinkedlist-replacement

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