- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我最近读了一篇关于 EnumMap
的文章.上面写道“使用 EnumMap 带来了针对枚举键的特定实现的好处,简而言之,EnumMap
专门针对枚举键优化了 Map 实现。”
还写到“枚举是使用数组实现的,常见操作的结果在恒定时间内。因此,如果您正在考虑高性能Map
,EnumMap
可能是枚举数据的不错选择。”
最佳答案
查看 documentation for EnumMap :
A specialized
Map
implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collections views (
keySet()
,entrySet()
, andvalues()
).
简而言之,一个 EnumMap
只是一个数组,属于 map 值的类型。换句话说,一个 EnumMap<SomeEnum, SomeValue>
, 将只是一个 SomeValue[]
.
您可能会问,索引是如何分配的?它们按枚举的自然顺序分配。示例:
enum Day {
MON, TUE, WED, THU, FRI, SAT, SUN
}
上述枚举具有以下自然顺序。
MON TUE WED THU FRI SAT SUN
0 1 2 3 4 5 6
因此,类似 map.put(Day.FRI, "Yay!")
的操作实际上可以看作:
array[4] = "Yay!";
数组访问是一个常量时间操作,这就是为什么 EnumMap
它的好处也是如此。查找 ( get()
) 的工作方式相同。
关于java - 由 EnumMap/EnumSet 完成的优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637288/
有许多关于类似错误的类似问题,但它们都是关于在尝试创建该类型的 EnumSet 之前枚举类型尚未初始化的特定情况。我的问题非常非常基本。无论我尝试做的事情多么简单,我都无法获得任何 EnumSet 方
我有一个枚举集 public static Set getTypes() { return EnumSet.of( TYPE_1, TYPE_2
此代码可以工作,但带有 try/catch 框 。 public enum AccentuationUTF8 {/** */ é, /** */è, /** */ç, /** */à,
我有两个 EnumSet。 我想将某些值从一个对象转移到另一个对象,但在两个对象中保留那些被视为“不可移动”的值。示例代码... Public enum MaterialTypes { ST
假设我想做如下的事情: public static Set setOf(V... elem) { Set set = null; if (elem[0] instanceof En
我刚刚开始学习 Java,并且正在研究一个可与一副纸牌配合使用的简单应用程序。目前,我正在尝试用枚举实例化一个 Suit 类。我想使用 EnumSet 功能,但我真的无法理解这里发生的事情。 我已经查
旧方法,如果我们想在一些复杂的位掩码上切换,我们可以很容易地这样做(我头脑中的一个随机示例只是为了演示这个问题): private static final int MAN = 0x0000000
我刚刚花了几个小时调试我的应用程序,而且我相信我偶然发现了一个(另一个 o_O)Java 错误...嗅探...我希望不是,因为这会让人难过:( 我正在做以下事情: 创建带有一些标志的 EnumSet
我正在写 writeToParcel方法和我的模型有一个 EnumSet作为属性。我不知道哪个out.write....()我必须使用这种类型吗? 最佳答案 EnumSet 实现了 Serializa
我正在读取一个二进制文件,该文件的值存储在位掩码中,包括 1 字节 位掩码 和 2 字节 位掩码。掩码中的每一位都充当一个开关,指示事件发生的位置。 Example of 1 Byte mask: 0
javadoc还有这个question都强调 Enum sets are represented internally as bit vectors. 现在我想知道 - 通过网络将(标准 Java 序
我有两个实现通用接口(interface)的枚举。我已尝试声明接口(interface),因此只有枚举可以实现它。 interface CommonEnumInterface> { /** I
我找不到 Immutable EnumSet 的版本.两个问题: 我可以在普通的 Guava ImmutableSet 中使用枚举吗? 如果可以的话,使用 ImmutableSet 而不是 EnumS
我正在努力解决 EnumSet因为它令人惊讶地没有简单的构造函数,而且它的方法也不喜欢空值。 我想出什么: EnumSet x = EnumSet.copyOf(Collections.emptySe
我有以下例子: import java.util.EnumSet; import java.util.Iterator; public class SizeSet { public stati
ConcurrentSkipListSet public class ConcurrentSkipListSet extends AbstractSet implements Navigabl
我正在使用 EnumSet 来执行按位权限操作,如《Effective Java》第 32 项中所建议的那样。这很好且简洁,但我需要提取数字位值,以便与需要数字的旧系统进行交互值(value)。使用
我想在一个枚举中存储不同的枚举类型。 这是正确(最短)的方法吗? public enum MyEnums { all(EnumSet.of(Color.red, Shape.round));
我正在尝试找到一种方法来使用该枚举类中的多个对象填充数组或其他一些数据结构(例如 EnumSet)。例如,我有一个枚举类,其中包含周一、周二、周三、周四等值。我想要做的是找到一种方法来多次使用这些值填
我正在尝试构建一个“平面文件阅读器”。这些文件将有几个相关的列,例如“客户姓名”、“电话”等,但每个平面文件的列号都不同;此外,某些平面文件将具有其他文件没有的列。 我希望 FlatFileReade
我是一名优秀的程序员,十分优秀!