Scala 为什么';设置';方法混淆ListBuffer中元素的顺序?
在scala中,为什么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
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
集合。其中的元素按添加顺序遍历。