何时在Java中使用枚举或集合 [英] When to use Enum or Collection in Java
本文介绍了何时在Java中使用枚举或集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在什么情况下,一个枚举比例如一个保证唯一元素的集合(java.util.Set的一个实现者,我猜...)?
(这是从我以前的问题)
解决方案
基本上,当一个定义良好的固定值这在编译时是已知的。
您可以非常容易地使用枚举作为一个集合(使用 EnumSet ),它允许您定义行为,通过名称引用元素,切换它们等。 / p>
Under what circumstances is an enum more appropriate than, for example, a Collection that guarantees unique elements (an implementer of java.util.Set, I guess...)?
(This is kind of a follow up from my previous question)
解决方案
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.
这篇关于何时在Java中使用枚举或集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文