Java 枚举值()。长度与私有字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1741708/
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
Enum values().length vs private field
提问by Micha? Ziober
I have enumeration like this:
我有这样的枚举:
public enum Configuration {
XML(1),
XSLT(10),
TXT(100),
HTML(2),
DB(20);
private final int id;
private Configuration(int id) {
this.id = id;
}
public int getId() { return id; }
}
Sometimes I need to check how many fields I have in enumeration. What is the best solution? Should I use a method "values().length"? Or maybe, I must create constant field in enumeration like this:
有时我需要检查枚举中有多少个字段。最好的解决方案是什么?我应该使用“values().length”方法吗?或者,我必须在枚举中创建常量字段,如下所示:
public enum Configuration {
XML(1),
XSLT(10),
TXT(100),
HTML(2),
DB(20);
private final int id;
private Configuration(int id) {
this.id = id;
}
public int getId() { return id; }
public static final int Size = 5;
}
What is the fastest and more elegant solution?
什么是最快、更优雅的解决方案?
采纳答案by Jon Skeet
Using values().length
will create a new copy of the array every time you call it. I sometimes create my own List
(or set, or map, whatever I need) to avoid this pointless copying. I wouldn'thard-code it though... if you only need the size, I'd just use:
values().length
每次调用它时,使用都会创建数组的新副本。我有时会创建自己的List
(或集合或地图,无论我需要什么)以避免这种毫无意义的复制。不过,我不会对其进行硬编码……如果您只需要大小,我将只使用:
private static final int size = Configuration.values().length;
at the end. By the time that is evaluated, all the values will have been initialized. This avoids the DRY and inconsistency concerns raised in other answers.
在末尾。到评估时,所有值都将被初始化。这避免了其他答案中提出的 DRY 和不一致问题。
Of course, this is a bit of a micro-optimisation in itself... but one which ends up with simplercode in the end, IMO. Calling values().length
from elsewhere doesn't express what you're interested in, which is justthe size of the enum - the fact that you get at it through an array of values is incidental and distracting, IMO.
当然,这本身就是一种微优化……但最终会得到更简单的代码,IMO。values().length
从其他地方调用并不能表达您对什么感兴趣,这只是枚举的大小 - 您通过一系列值获得它的事实是偶然和分散注意力的,IMO。
An alternative to using values()
is to use EnumSet.allOf().size()
which for small enums will be pretty cheap - but again, it's not as readable as just having a size
field.
使用的替代方法values()
是将EnumSet.allOf().size()
which 用于小型枚举将非常便宜 - 但同样,它不像只有一个size
字段那样可读。
回答by Adamski
I would recommend using values().length
. This is far more elegant and the performance overhead versus using a constant will be negligable. Also, you eliminate the risk of the constant ever becoming out of step with the actual length of the enumeration.
我会推荐使用values().length
. 这要优雅得多,与使用常量相比,性能开销可以忽略不计。此外,您还消除了常数与枚举的实际长度不一致的风险。
回答by PaulJWilliams
By storing the count you're violating the DRY principle, so unless you have a very good reason, you shouldn't.
通过存储计数,您违反了DRY 原则,因此除非您有充分的理由,否则您不应该这样做。
回答by Hudson Pena Magalh?es
Another approach is to use a constant initialized on top of the values() method.
另一种方法是使用在 values() 方法之上初始化的常量。
public enum Colors {
BLUE, GREEN, FUCHSIA;
public static int length = Colors.values().length;
}
This way you have an automatically updated constant and still avoid that "values()" overhead.
这样你就有了一个自动更新的常量,并且仍然避免了“values()”的开销。