枚举值().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; }
}
有时我需要检查枚举中有多少个字段。最好的解决方案是什么? 我应该使用方法“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;
}
什么是最快且更优雅的解决方案?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每次调用时,使用
values().length
都会创建数组的新副本。我有时会创建自己的List
(或设置或映射,无论我需要什么)以避免这种毫无意义的复制。不过,我不会对其进行硬编码...如果您只需要大小,我只需在末尾使用:。在评估时,所有值都将被初始化。这避免了其他答案中提出的 DRY 和不一致问题。
当然,这本身就是一种微观优化……但最终会得到更简单的代码,IMO。从其他地方调用
values().length
并不能表达您感兴趣的内容,这只是枚举的大小 - 事实上您是通过在我看来,一系列的价值观是偶然的和分散注意力的。使用
values()
的替代方法是使用EnumSet.allOf().size()
,对于小型枚举来说,它会非常便宜 - 但同样,它的可读性不如只是有一个size
字段。Using
values().length
will create a new copy of the array every time you call it. I sometimes create my ownList
(or set, or map, whatever I need) to avoid this pointless copying. I wouldn't hard-code it though... if you only need the size, I'd just use: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.
Of course, this is a bit of a micro-optimisation in itself... but one which ends up with simpler code in the end, IMO. Calling
values().length
from elsewhere doesn't express what you're interested in, which is just the size of the enum - the fact that you get at it through an array of values is incidental and distracting, IMO.An alternative to using
values()
is to useEnumSet.allOf().size()
which for small enums will be pretty cheap - but again, it's not as readable as just having asize
field.我建议使用
values().length
。这要优雅得多,并且与使用常量相比,性能开销可以忽略不计。此外,还消除了常量与枚举的实际长度不一致的风险。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.通过存储计数,您违反了DRY 原则,所以除非您有一个非常有充分的理由,你不应该。
By storing the count you're violating the DRY principle, so unless you have a very good reason, you shouldn't.
另一种方法是使用在 value() 方法之上初始化的常量。
这样你就有了一个自动更新的常量,并且仍然避免了“values()”开销。
Another approach is to use a constant initialized on top of the values() method.
This way you have an automatically updated constant and still avoid that "values()" overhead.