Scala Map 实现按插入顺序保留条目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3835743/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:27:30  来源:igfitidea点击:

Scala Map implementation keeping entries in insertion order?

scalascala-collectionsscala-2.8ordered-map

提问by ebruchez

In Java, I use LinkedHashMapfor this purpose. The documentation of Java's LinkedHashMapis very clear that it has "predictable iteration order" and I need the same in Scala.

在 Java 中,我LinkedHashMap用于此目的。Java 的文档LinkedHashMap非常清楚,它具有“可预测的迭代顺序”,而我在 Scala 中也需要相同的内容。

Scala has ListMapand LinkedHashMap, but the documentation on what they do exactly is poor.

Scala 有ListMapLinkedHashMap,但是关于它们究竟做什么的文档很差。

Question: Is Scala's LinkedHashMapor ListMapthe implementation to use for this purpose? If not, what other options are available besides using the Java's LinkedHashMapdirectly?

问题:ScalaLinkedHashMapListMap用于此目的的实现吗?如果没有,除了LinkedHashMap直接使用 Java 之外,还有哪些其他选项可用?

回答by Randall Schulz

From the LinkedHashMapScaladoc page:

LinkedHashMapScaladoc 页面:

  • "This class implements mutable maps using a hashtable. The iterator and all traversal methods of this class visit elements in the order they were inserted."
  • “这个类使用哈希表实现可变映射。这个类的迭代器和所有遍历方法按照元素插入的顺序访问元素。”

回答by Reid Spencer

The difference between the two is that LinkedHashMapis mutable while ListMapis immutable. Otherwise they both are MapLikeand also preserve insertion order.

两者之间的区别LinkedHashMap是可变的,而不可变的ListMap。否则,它们都是MapLike并且也保留插入顺序。

回答by Michelle

For LinkedHashMap, the answer is pretty clear that it preserves the order of insertion.

对于 LinkedHashMap,答案很清楚,它保留了插入的顺序。

But for ListMap, it seems that there are some confuses here.

但是对于ListMap,这里似乎有些混乱。

Firstly, there are two ListMap.

首先,有两个ListMap。

  • scala.collection.mutable.ListMap
  • scala.collection.immutable.ListMap.
  • scala.collection.mutable.ListMap
  • scala.collection.immutable.ListMap。

Secondly, the document for ListMap has something wrong as far as I tried.

其次,据我尝试,ListMap 的文档有问题。

mutable.ListMap

可变的.ListMap

The actual order is not the insertion order as it says.

实际顺序不是它所说的插入顺序。

And it is not the inverse order of insertion, either. The result I tried is [forth, second, first, third]

它也不是插入的相反顺序。我试过的结果是[第四、第二、第一、第三]

A simple mutable map backed by a list, so it preserves insertion order.

一个由列表支持的简单可变映射,因此它保留了插入顺序。

immutable.ListMap

immutable.ListMap

As the document saying that, the order is the insertion order.

正如文档所说,顺序是插入顺序。

One thing to notice is that it is stored internally in reversed insertion order. And the internally stored order and the iterable/traversal order are two things. The internally stored order decides the time complexity of lookup methods such as head/last/tail/init/.

需要注意的一件事是它以相反的插入顺序在内部存储。内部存储顺序和可迭代/遍历顺序是两件事。内部存储的顺序决定了head/last/tail/init/等查找方法的时间复杂度。

This class implements immutable maps using a list-based data structure. List map iterators and traversal methods visit key-value pairs in the order whey were first inserted.

Entries are stored internally in reversed insertion order, which means the newest key is at the head of the list.

此类使用基于列表的数据结构实现不可变映射。列表映射迭代器和遍历方法按照第一次插入乳清的顺序访问键值对。

条目以相反的插入顺序在内部存储,这意味着最新的键位于列表的头部。

回答by Andrew Norman

  • LinkedHashmap is in the order it was added
  • (immutable) ListMap is in the backward order it was added (i.e. the last one added is first)
  • LinkedHashmap 按照添加顺序
  • (不可变)ListMap 是按其添加的倒序顺序排列的(即最后添加的是第一个)

LinkedHashmap is only implemented as a mutable map ListMaps are implemented in both the mutable and immutable packages, however only the immutable ListMaps maintain the backwards ordering. (mutable listmaps do not maintain order)

LinkedHashmap 仅作为可变映射实现 ListMaps 在可变和不可变包中都实现,但是只有不可变的 ListMaps 保持向后排序。(可变列表映射不维护顺序)

回答by Nandakishore

ListMapdoesn't preserves the order of insertion.

ListMap不保留插入顺序。

enter image description here

在此处输入图片说明

Only LinkedHashMapmaintains the order of elements the way they are inserted.

LinkedHashMap保持元素插入的顺序。

enter image description here

在此处输入图片说明

If you want to maintain order in Lists otherthan Map you can use LinkedList

如果您想在 Map 以外的列表中维护订单,您可以使用 LinkedList

enter image description here

在此处输入图片说明

回答by ebruchez

Scala 2.13 is introducing two new immutable implementations of Mapwhich keep insertion order: VectorMapand SeqMap. See this PR:"

Scala 2.13 引入了两个新的不可变实现,Map它们保持插入顺序:VectorMapSeqMap. 看到这个公关:”

Currently there isn't any known immutable map which also maintains key insertion order while keeping effectively constant lookup time on key, so the only known implementations are done by combining a Vector with a HasMap (or in Scala's case HashMap/ChampHashMap)

目前还没有任何已知的不可变映射在保持键插入顺序的同时有效地保持键上的恒定查找时间,因此唯一已知的实现是通过将 Vector 与 HasMap(或在 Scala 的情况下为 HashMap/ChampHashMap)结合来完成的

As of writing, Scala 2.13 is still scheduled to be released in 2018.

在撰写本文时,Scala 2.13 仍计划于 2018 年发布。