Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 为什么';设置';方法混淆ListBuffer中元素的顺序?_Scala_Collections_Set_Listbuffer - Fatal编程技术网

Scala 为什么';设置';方法混淆ListBuffer中元素的顺序?

Scala 为什么';设置';方法混淆ListBuffer中元素的顺序?,scala,collections,set,listbuffer,Scala,Collections,Set,Listbuffer,在scala中,为什么toSet()方法会混淆集合中元素的顺序(ListBuffer) 我可以使用哪个集合来确保每个元素的唯一性并保持其原始顺序?因为抽象作为的子类,无法保证元素在其中的顺序: A traversable class might or might not have two properties: strictness and orderedness. Neither is represented as a type. ... If the class is not order

在scala中,为什么
toSet()
方法会混淆集合中元素的顺序(
ListBuffer

我可以使用哪个集合来确保每个元素的唯一性并保持其原始顺序?

因为抽象作为的子类,无法保证元素在其中的顺序:

A traversable class might or might not have two properties: strictness and orderedness. Neither is represented as a type.
 ...
 If the class is not ordered, foreach can visit elements in different orders for different runs (but it will keep the same order in the same run).'

更确切地说,关于元素“损坏”的原因:
toSet
方法用一些现有集合构造一个新集合。它使用此新集合的默认集合实现。默认的集合实现基于哈希表。在哈希表中,元素的顺序是未定义的。

如果需要元素唯一性,则存在
mutable.LinkedHashSet
集合。其中的元素按添加顺序遍历。