何时在 Java 中使用 Enum 或 Collection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/481068/
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
When to use Enum or Collection in Java
提问by brabster
回答by Jon Skeet
Basically when it's a well-defined, fixed set of values which are known at compile-time.
基本上,当它是在编译时已知的一组明确定义的固定值时。
You can use an enum as a set very easily (with EnumSet) and it allows you to define behaviour, reference the elements by name, switch on them etc.
您可以非常轻松地将枚举用作集合(使用EnumSet),它允许您定义行为、按名称引用元素、打开它们等。
回答by duffymo
When the elements are known up front and won't change, an enum is appropriate.
当元素预先知道并且不会改变时,枚举是合适的。
If the elements can change during runtime, use a Set.
如果元素可以在运行时更改,请使用 Set。
回答by duffymo
I am no java guru, but my guess is to use enumeration when you want to gurantee a certain pool of values, and to use a collection when you want to gurantee uniqueness. Example would be to enumerate days of the week (cant have "funday") and to have a collection of SSN (generic example i know!)
我不是 Java 专家,但我的猜测是,当您想要保证某个值池时使用枚举,而当您想要保证唯一性时使用集合。示例是枚举一周中的几天(不能有“funday”)并收集 SSN(我知道的通用示例!)
回答by brabster
Great responses - I'll try and summarise, if just for my own reference - it kinda looks like you should use enums in two situations:
很好的回应 - 我会尝试总结,如果只是为了我自己的参考 - 看起来你应该在两种情况下使用枚举:
All the values you need are known at compile time, and either or both of the following:
您需要的所有值在编译时都是已知的,以及以下任一或两个值:
- you want better performance than your usual collection implementations
- you want to limit the potential values to those specified at compile time
- 你想要比通常的集合实现更好的性能
- 您希望将潜在值限制为在编译时指定的值
With the Collection over enumeration links that Jon gave, you can get the benefits of enum performance and safety as an implementation detail without incorporating it into your overall design.
使用 Jon 提供的 Collection over enumeration 链接,您可以获得枚举性能和安全性的好处作为实现细节,而无需将其合并到您的整体设计中。
Community wiki'd, please do edit and improve if you want to!
社区 wiki'd,如果您愿意,请进行编辑和改进!
回答by Peter Lawrey
Note: you can have both with an EnumSet.
注意:您可以同时使用 EnumSet。
回答by Jurgen Hannaert
In some situations your business requires the creation of new items, but at the same time business logic based on some fixed items. For the fixed ones you want an enum, the new ones obviously require some kind of collection/db.
在某些情况下,您的业务需要创建新项目,但同时基于一些固定项目的业务逻辑。对于您想要枚举的固定类型,新的显然需要某种集合/数据库。
I've seen projects using a collection for this kind of items, resulting in business logic depending on data which can be deleted by the user. Never do this, but do create a separate enum for the fixed ones and a collection for the others, just as required.
我已经看到项目使用此类项目的集合,导致业务逻辑取决于用户可以删除的数据。永远不要这样做,但要根据需要为固定的枚举创建一个单独的枚举,为其他创建一个集合。
An other solution is to use a collection with immutable objects for the fixed values. These items could also reside in a db, but have an extra flag so users cannot update / delete it.
另一种解决方案是使用具有固定值的不可变对象的集合。这些项目也可以驻留在数据库中,但有一个额外的标志,因此用户无法更新/删除它。

