简体   繁体   English

带枚举键的稀疏映射

[英]Sparse Map with Enum keys

I need to create a Map with enum keys where only a small fraction of the enum constants will be actually inserted. 我需要使用枚举键创建一个Map,在该映射中实际上只插入一小部分枚举常量。 What is the best approach? 最好的方法是什么? An EnumMap would be inefficient if the length of its underlying array is equal to the total number of enum constants. 如果EnumMap的基础数组的长度等于枚举常量的总数,则EnumMap的效率将会降低。

I suggest using an ordinary HashMap . 我建议使用普通的HashMap

Computing hashes for enums is both easy and cheap. 为枚举计算哈希既简单又便宜。 There should be no significant memory overhead, since you are not duplicating the enum objects, but instead creating multiple references to the same object. 应该没有明显的内存开销,因为您不是在复制枚举对象,而是创建对同一对象的多个引用。 For this reason, there should be little difference between storing an integer key and storing a reference to an enum object. 因此,在存储整数键和存储对枚举对象的引用之间应该没有什么区别。

I'd also go with either a HashMap or a TreeMap (depending on whether you need a deterministic Iterator or not). 我还要使用HashMapTreeMap (取决于您是否需要确定性迭代器)。 Since your data is sparse, any real or imagined overhead is unlikely to be a significant performance hurdle. 由于您的数据稀疏,因此任何实际或预期的开销都不太可能成为严重的性能障碍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM