要使用哪个 Scala 可变列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11049213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which scala mutable list to use?
提问by Karel Bílek
This is a followup question to No Scala mutable list
这是No Scala mutable list的后续问题
I want to use a mutable list in Scala. I can chose from
我想在 Scala 中使用可变列表。我可以选择
scala.collection.mutable.DoubleLinkedListscala.collection.mutable.LinkedListscala.collection.mutable.ListBufferscala.collection.mutable.MutableList
scala.collection.mutable.DoubleLinkedListscala.collection.mutable.LinkedListscala.collection.mutable.ListBufferscala.collection.mutable.MutableList
Which is nice, but what is the "standard", recommended, idiomatic scala way? I just want to use a list that I can add things to on the back.
哪个很好,但什么是“标准”、推荐、惯用的 Scala 方式?我只想使用一个列表,我可以在后面添加内容。
In my case, I am using a HashMap, where the "lists" (I am meaning it in general sense) will be on value side. Then, I am reading something from a file and for every line, I want to find the right list in the hashmap and append the value to the list.
就我而言,我使用的是 HashMap,其中“列表”(我的意思是一般意义上的)将在价值方面。然后,我从文件中读取一些内容,对于每一行,我想在哈希图中找到正确的列表并将值附加到列表中。
采纳答案by Daniel Werner
For the sake of readers visiting this old question: The documentation's Concrete Mutable Collection Classessection has an overview of mutable list classes, including explanations on when to use which one.
为了读者访问这个老问题:文档的具体可变集合类部分概述了可变列表类,包括何时使用哪个类的解释。
回答by axel22
Depends what you need.
取决于你需要什么。
DoubleLinkedListis a linked list which allows you to traverse back-and-forth through the list of nodes. Use its prevand nextreferences to go to the previous or the next node, respectively.
DoubleLinkedList是一个链表,它允许您来回遍历节点列表。使用它的prev和next引用分别转到上一个或下一个节点。
LinkedListis a singly linked list, so there are not prevpointers - if you only traverse to the next element of the list all the time, this is what you need.
LinkedList是一个单链表,所以没有prev指针——如果你一直只遍历列表的下一个元素,这就是你需要的。
EDIT: Note that the two above are meant to be used internallyas building blocks for more complicated list structures like MutableLists which support efficient append, and mutable.Queues.
编辑:请注意,上面的两个是在内部用作构建块以构建更复杂的列表结构,例如MutableList支持高效追加的mutable.Queues和s。
The two collections above both have linear-time append operations.
上面的两个集合都具有线性时间追加操作。
ListBufferis a buffer class. Although it is backed by a singly linked list data structure, it does not expose the nextpointer to the client, so you can only traverse it using iterators and the foreach.
Its main use is, however, as a buffer and an immutable list builder - you append elements to it via +=, and when you call result, you very efficiently get back a functional immutable.List. Unlike mutable and immutable lists, both append and prepend operations are constant-time - you can append at the end via +=very efficiently.
ListBuffer是一个缓冲区类。尽管它由单链表数据结构支持,但它不会将next指针暴露给客户端,因此您只能使用迭代器和foreach. 然而,它的主要用途是作为缓冲区和不可变列表构建器 - 您可以通过 将元素附加到它+=,当您调用时result,您可以非常有效地返回一个功能性的immutable.List. 与可变和不可变列表不同,追加和前置操作都是常数时间 - 您可以+=非常有效地在末尾追加。
MutableListis used internally, you usually do not use it unless you plan to implement a custom collection class based on the singly linked list data structure. Mutable queues, for example, inherit this class. MutableListclass also has an efficient constant-time append operation, because it maintains a reference to the last node in the list.
MutableList内部使用,除非你打算实现一个基于单向链表数据结构的自定义集合类,否则你通常不会使用它。例如,可变队列继承了这个类。MutableList类还有一个高效的常量时间追加操作,因为它维护对列表中最后一个节点的引用。
回答by drexin
If you want to append items you shouldn't use a Listat all. Lists are good when you want to prepend items. Use ArrayBufferinstead.
如果你想追加项目,你根本不应该使用 a List。List当您想要预先添加项目时,s 很好。使用ArrayBuffer来代替。
回答by Daniel C. Sobral
I just want to use a list that I can add things to on the back.
我只想使用一个列表,我可以在后面添加内容。
Then choose something that implements Growable. I personally suggest one of the Bufferimplementations.
然后选择实现Growable. 我个人建议其中一种Buffer实现。
I stay away from LinkedListand DoubleLinkedList, as they are present mainly as underlying implementation of other collections, but have quite a few bugs up to Scala 2.9.x. Starting with Scala 2.10.0, I expect the various bug fixes have brought them up to standard. Still, they lack some methods people expect, such as +=, which you'll find on collections based on them.
我远离LinkedListand DoubleLinkedList,因为它们主要作为其他集合的底层实现存在,但在 Scala 2.9.x 之前有很多错误。从 Scala 2.10.0 开始,我希望各种错误修复使它们达到标准。尽管如此,它们仍然缺乏人们期望的一些方法,例如+=,您可以在基于它们的集合中找到这些方法。

