简体   繁体   English

套装的“toArray”是否具有确定性?

[英]Is a set's “toArray” deterministic?

Obviously, sets do not have any kind of ordering, so I cannot expect any specific ordering if I do 显然,集合没有任何排序,所以如果我这样做,我不能指望任何特定的排序

String[] string = mySet.toArray();

However, I am faced with a use case where I don't care what ordering the string array is in, but I DO need it to be the case that if two sets are equal to each other, then: 但是,我遇到了一个用例,我不关心字符串数组的排序,但是我需要它是这样的情况:如果两个集合彼此相等,那么:

StringUtils.join(mySet.toArray(),',');

will produce the same exact string for those sets, always, no matter how many times I run the program assuming I stick with the same code. 无论我运行程序多少次,假设我坚持使用相同的代码,我将始终为这些集合生成相同的字符串。

Do I have this guarantee? 我有这个保证吗?

Similarly, does this whole true for the order that elements appear for a given Set in an iterator? 类似地,这对于迭代器中给定Set的元素出现的顺序是否完全正确?

In general, you cannot guarantee the order of a Set as you mention, so even if it does work now, it may not in the future. 一般来说,你不能保证你提到的Set的顺序,所以即使它现在有用,也可能在将来不行。 However, you can guarantee the order of a LinkedHashSet so you can just use that. 但是,您可以保证LinkedHashSet的顺序,以便您可以使用它。 This only works if the sets had the elements inserted in the same order though. 这仅在集合具有以相同顺序插入的元素时才有效。 If you don't have that situation, you probably just have to sort the set and print it. 如果您没有这种情况,您可能只需对该集进行排序并打印即可。

No, absolutely not. 不,绝对不是。 For a trivial example, consider: 对于一个简单的例子,考虑:

 LinkedHashSet<String> set1 = new LinkedHashSet<String>();
 set1.add("x");
 set1.add("y");

 LinkedHashSet<String> set2 = new LinkedHashSet<String>();
 set2.add("y");
 set2.add("x");

Those two sets are equal, but because LinkedHashSet preserves insertion order, it's guaranteed that their iterators will return values in a different order - and I'd expect toArray to have the same effect. 这两个集是相同的,但是因为LinkedHashSet保留了插入顺序,所以它保证它们的迭代器将以不同的顺序返回值 - 而且我希望toArray具有相同的效果。

Technically, no you don't have a guarantee. 从技术上讲,没有你没有保证。 Set is an Interface. Set是一个接口。 Implementations will vary and may or may not obey this requirement. 实施方式会有所不同,可能会也可能不会遵守此要求。

Force the issue by sorting the array yourself after extracting the results from the Set. 从Set中提取结果后,通过自己排序数组来强制解决问题。

That would be completely dependent on the Set implementation. 这完全取决于Set实现。 Set is an interface, not a class, and toArray() does not guarantee anything except that it must match the implementation's Iterator. Set是一个接口,而不是一个类,而toArray()除了必须与实现的Iterator匹配外,不保证任何东西。

From the javadoc for Set.toArray(): 从Set.toArray()的javadoc:

If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. 如果此set对其迭代器返回的元素的顺序做出任何保证,则此方法必须以相同的顺序返回元素。

You do not have this guarantee. 你没有这个保证。 It may be that you will always see this work, because I suspect most implementations of Set.toArray() will produce the same ordering for equal sets, but you cannot rely on this. 可能你总是会看到这个工作,因为我怀疑Set.toArray()大多数实现都会为相等的集生成相同的顺序,但是你不能依赖它。

Nor can you rely on the order from an iterator. 你也不能依赖迭代器的顺序。

Sets are completely and wholly unordered. 集合完全无序。 You might, due to implementation behaviour, get something that looks like a consistent order out of them, but this is not in any way to be relied upon. 由于实现行为,您可能会从它们中获得看起来像一致顺序的内容,但这绝不是任何依赖的方式。

For HashSet you can add element in the same order and get different orders based on the capacity and load factor. 对于HashSet,您可以按相同顺序添加元素,并根据容量和负载因子获取不同的订单。

Here is an example where you can have many different orders from the same collection. 这是一个示例,您可以从同一个集合中获得许多不同的订单。

http://vanillajava.blogspot.co.uk/2011/09/order-of-elements-in-hash-collection.html http://vanillajava.blogspot.co.uk/2011/09/order-of-elements-in-hash-collection.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM