2011-09-19 71 views

回答

11

因为set抽象,作为traversable的子类,没有关于元素之内举行的顺序保证:

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方法构造了一个从现有的一些收藏中收集新的集合。它使用这个新集合集合的默认集合实现。默认设置实现基于哈希表。在散列表中,元素的顺序是未定义的。

+3

如果您需要元素唯一性,则有'mutable.LinkedHashSet'集合。其中的元素按照添加的顺序遍历。 – axel22