简体   繁体   English

来自 Set.of(...) 的随机迭代顺序

[英]Random Iteration order from Set.of(…)

Hi I am learning the new features of Java 9 and was reading new Collections API's.嗨,我正在学习 Java 9 的新功能,并且正在阅读新的 Collections API。 I am not sure why I am getting random iteration order while creating an immutable set from two instances of JDK.我不确定为什么在从 JDK 的两个实例创建不可变集时得到随机迭代顺序。

JDK 1: jshell> Set.of("a","b","c")
       $96 ==> [a, b, c]

JDK2 : jshell> Set.of("a","b","c")
       $68 ==> [a, c, b]

I don't see the same for List.of() overloads.Can I do some customisation like changing some system properties to guarantee a consistent ordering here?对于 List.of() 重载,我看不到相同的内容。我可以进行一些自定义,例如更改某些系统属性以保证此处的顺序一致吗?

Arbitrary Set implementations have no ordering, so any iteration order encountered when iterating over the elements of a Set returned by Set.of() is correct, and you shouldn't rely on it.任意Set实现没有排序,因此在迭代Set.of()返回的Set的元素时遇到的任何迭代顺序都是正确的,您不应该依赖它。 It's an implementation detail, and different JDK versions can produce different iteration orders.这是一个实现细节,不同的JDK版本可以产生不同的迭代顺序。

If you require a consistent ordering, either don't use Set s, or use Set s that guarantee order, such as TreeSet .如果您需要一致的顺序,请不要使用Set s,或者使用保证顺序的Set s,例如TreeSet

List.of() has a different behavior, since List s have ordering. List.of()具有不同的行为,因为List具有排序。 In the List returned by List.of("a","b","c") , "a" will always be the first element, "b" the second element and "c" the third.List.of("a","b","c")返回的List中,“a”始终是第一个元素,“b”是第二个元素,“c”是第三个元素。

As Eran already explained, the iteration order of a Set is not specified.正如Eran已经解释的那样,没有指定Set的迭代顺序。

To avoid that other programs rely on the iteration order, the JVM will create a salt at startup and use that to randomize the iteration order of their internal Set and Map implementations:为了避免其他程序依赖于迭代顺序,JVM 将在启动时创建一个 salt,并使用它来随机化其内部SetMap实现的迭代顺序:

 /** * A "salt" value used for randomizing iteration order. This is initialized once * and stays constant for the lifetime of the JVM. It need not be truly random, but * it needs to vary sufficiently from one run to the next so that iteration order * will vary between JVM runs. */ private static final long SALT32L;

Source 资源

In short: They did additional work to make the iteration order "random", so things will break if people start to rely on the order.简而言之:他们做了额外的工作来使迭代顺序“随机”,所以如果人们开始依赖这个顺序,事情就会中断。

And some programs do rely on the iteration order of HashSet for example - which makes it hard to improve HashSet now without breaking programs that rely on the order.例如,有些程序确实依赖于HashSet的迭代顺序——这使得现在很难在不破坏依赖顺序的程序的情况下改进HashSet

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

相关问题 如何在Java 9 Set.of中保持顺序 - How to keep order in Java 9 Set.of 从集合中以随机顺序获取所有元素 - get ALL elements from set in random order 有没有办法使用 IntelliJ 重构 Set 初始化以使用 Set.of()? - Is there a way to refactor a Set initialization to use Set.of() using IntelliJ? 如果元素是重复的,为什么Set.of()会抛出IllegalArgumentException? - Why does Set.of() throw an IllegalArgumentException if the elements are duplicates? 如何修复 Set.of() 找不到符号:方法 of(String,String)? - How to fix Set.of() cannot find symbol: method of(String,String)? JDK 11 Generics 使用 Set.of 时的问题 - JDK 11 Generics Issue when using Set.of 从Guava ImmutableTable派生的视图的迭代顺序 - Iteration order for views derived from Guava ImmutableTable Set.of(E... elements) - 它使用的是哪个 Set 实现? 它与 new HashSet<>() 有什么关系? - Set.of(E... elements) - which Set implementation is it using? and what is its relation to new HashSet<>()? Poset迭代顺序/部分有序集的自定义迭代器实现 - Poset iteration order / custom iterator implementation for a partially ordered set 使用工厂方法Set.of()和Map.of()创建的集合和地图的时间复杂度是多少? - What is a time complexity for sets and maps created with a factory methods Set.of() and Map.of()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM