Java enum.values() - 是返回的枚举确定性的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3820149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:27:18  来源:igfitidea点击:

enum.values() - is an order of returned enums deterministic

javaenumsspecifications

提问by Skarab

I have a enum SOME_ENUM:

我有一个枚举SOME_ENUM

public enum SOME_ENUM {
  EN_ONE,
  EN_TWO,
  EN_THREE;
}

Will SOME_ENUM.values()always return the enums in the order of enum declarations: EN_ONE, EN_TWO, EN_THREE? Is it a rule or it is not guaranteed to be not changed in the next JDK releases?

SOME_ENUM.values()始终按照枚举声明的顺序返回枚举: EN_ONE, EN_TWO, EN_THREE? 这是规则还是不能保证在下一个 JDK 版本中不会更改?

采纳答案by GaryF

The Java language specification uses this explicit language:

Java 语言规范使用这种显式语言:

@return an array containing the constants of this enum type, in the order they're declared [Source]

@return 包含此枚举类型常量的数组,按照它们声明的顺序[Source]

So, yes, they will be returned in declaration order. It's worth noting that the order might change over time if someone changes the class so be very careful about how you use this.

所以,是的,它们将按声明顺序返回。值得注意的是,如果有人更改了类,顺序可能会随着时间的推移而改变,因此在使用它时要非常小心。

回答by Bozho

Yes, it is a guaranteed to return them in that order.

是的,保证按该顺序返回它们。

However you should avoid relying on that, and on the ordinal()value, since it can change after inserting new items, for example.

但是,您应该避免依赖它和该ordinal()值,因为它可以在插入新项目后发生变化,例如。

回答by Péter T?r?k

It is determined by the order your values are declared in. However, there is no guarantee that you (or someone else) won't reorder / insert / remove values in the future. So you shouldn't rely on the order.

它由您的值声明的顺序决定。但是,不能保证您(或其他人)将来不会重新排序/插入/删除值。所以你不应该依赖订单。

Effective Java 2nd. Edition dedicates its Item 31 to a closely related topic: Use instance fields instead of ordinals:

有效的Java 2nd。版本将其 Item 31 专门用于一个密切相关的主题:使用实例字段而不是序数

Never derive a value associated with an enum from its ordinal; store it in an instance field instead.

永远不要从它的序数中导出与枚举相关的值;将其存储在实例字段中。

回答by Fletch

The other answers are good, but don't comment on this:

其他答案很好,但不要对此发表评论:

"Is it a rule or it is not guaranteed to be not changed in the next Jdk releases?"

“是规则还是不能保证在下一个Jdk版本中不会改变?”

I don't believe that guarantees on future JDKs exist, so you shouldn't even worry about them. There would be no way to enforce them, future JDK leads might just decide to reneg on such guarantees. It's like the Westminster system of parliament: "No Parliament can bind a future parliament."

我不相信对未来 JDK 的保证存在,所以你甚至不应该担心它们。没有办法强制执行它们,未来的 JDK 领导者可能会决定放弃这样的保证。这就像威斯敏斯特议会制度:“没有议会可以约束未来的议会。”

That said, the history of the JDK reveals excellent consistency. They don't make a lot of breaking changes, so you can be pretty confident that current specified(not just observed) behaviour will be preserved.

也就是说,JDK 的历史揭示了极好的一致性。它们不会进行很多破坏性更改,因此您可以非常确信当前指定的(不仅仅是观察到的)行为将被保留。