我的枚举正确吗?
在我们的整个项目中,我们都有这种枚举。 它们工作得很好,但我们不确定它们。
特别是使用 getDocumentType(String) 方法。
有没有办法避免对所有 Enums 字段进行迭代?
public enum DocumentType {
UNKNOWN("Unknown"),
ANY("Any"),
ASSET(Asset.class.getSimpleName()),
MEDIA(Media.class.getSimpleName()),
MEDIA35MM(Media.class.getSimpleName() + " 35mm");
private String label;
private DocumentType(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public static DocumentType getDocumentType(String label){
for(DocumentType documentType : DocumentType.values()){
if(documentType.getLabel().equals(label)){
return documentType;
}
}
return UNKNOWN;
}
}
编辑 : 检查 newacct 响应。 她也很好。
All over our project, we have this kind of enums. They works just fine, but we are not sure about them.
Specially with the getDocumentType(String) method.
Is there a way to avoid the iteration over all the Enums field ?
public enum DocumentType {
UNKNOWN("Unknown"),
ANY("Any"),
ASSET(Asset.class.getSimpleName()),
MEDIA(Media.class.getSimpleName()),
MEDIA35MM(Media.class.getSimpleName() + " 35mm");
private String label;
private DocumentType(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public static DocumentType getDocumentType(String label){
for(DocumentType documentType : DocumentType.values()){
if(documentType.getLabel().equals(label)){
return documentType;
}
}
return UNKNOWN;
}
}
Edit :
Check the newacct response. She's fine too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于编写枚举的限制,您将不得不在某个地方进行迭代。 在理想的情况下,您可以从 DocumentType 的构造函数中填充静态 Map,但这是不允许的。
我能建议的最好方法是在静态初始化程序中执行一次迭代,并将枚举存储在查找表中:
至少您不会每次都进行迭代,尽管我怀疑您是否会看到任何有意义的性能改进。
You're going to have to do that iteration somewhere, due to the restrictions in writing enums. In an ideal world, you would populate a static Map from within DocumentType's constructor, but that's not allowed.
The best I can suggest is performing the iteration once in a static initializer, and storing the enums in a lookup table:
At least you won't be doing the iteration every time, although I doubt you'll see any meaningful performance improvement.
据我所知(无论它的价值),这是做你想做的事的最好方法。
至少我会这样做。
如果您的
enum
数量显着增长(数百至数千),您可能需要将Strings
的Map
添加到enum
code> 使查找速度更快一些。 但对于您拥有的少量eunum
来说,这可能有点过分了。As far as I know (For what it's worth), that is the best way to do what you want.
That is how I would do it at least.
If your
enum
count grows significantly (couple hundred - thousands) you may want to add aMap
ing ofStrings
toenums
to do the look-up a little faster. But for the small amount ofeunums
you have, this may be overkill.如果字符串在编译时已知,并且它们是有效的标识符,则可以直接将它们用作枚举的名称:
然后通过
.valueOf()
获取它。 例如:If the strings are known at compile-time, and if they are valid identifiers, you can just use them as the names of the enums directly:
and then get it by
.valueOf()
. For example:对我来说看起来不错。
我会让迭代保持原样。 当然,您可以添加 Map<'label','DocumentType'> 实现枚举类并进行查找,但不会显着提高性能。
Looks fine to me.
I'd leave the iteration as it is. Sure you could add a Map<'label','DocumentType'> implementation to the enum class and do a lookup but it won't increase performance significantly.