When to use Enum or Collection in Java(何时在 Java 中使用 Enum 或 Collection)
问题描述
在什么情况下,枚举比保证唯一元素的 Collection(我猜是 java.util.Set 的实现者……)更合适?
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...)?
(这是我之前的 问题)
推荐答案
基本上当它是一个定义明确的、在编译时已知的固定值集时.
Basically when it's a well-defined, fixed set of values which are known at compile-time.
您可以非常轻松地将枚举用作集合(使用 EnumSet),它允许您定义行为、按名称引用元素、打开它们等.
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 中使用 Enum 或 Collection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:何时在 Java 中使用 Enum 或 Collection
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
