简体   繁体   English

为什么EnumSet是作为抽象类实现的,而EnumMap是作为具体类实现的?

[英]Why EnumSet is implemented as abstract class and EnumMap is implemented as concrete class?

我想知道,为什么EnumSet被实现为抽象类并且EnumMap被实现为具体类?

EnumSet actually has two implementations - one for enums with 64 or less elements (flags indicating presence of values in the set are stored as long ) and another one for other enums (flags are stored as long[] ). EnumSet实际上有两个实现 - 一个用于具有64个或更少元素的枚举(表示集合中存在值的标志存储为long ),另一个用于其他枚举(标志存储为long[] )。 Factory methods of EnumSet return one of them depending on the enum class passed in. EnumSet工厂方法根据传入的枚举类返回其中一个。

Such an optimization doesn't make sense for EnumMap (since array to store values is needed anyways), therefore EnumMap is a concrete class. 这样的优化对于EnumMap没有意义(因为无论如何都需要存储值的数组),因此EnumMap是一个具体的类。

EnumSet uses two implementations, RegularEnumSet and JumboEnumSet , depending on the size of your enum (64 elements being the threshold). EnumSet使用两个实现, RegularEnumSetJumboEnumSet ,具体取决于枚举的大小(64个元素是阈值)。 The factory methods delegate to the appropriate implementation. 工厂方法委托给适当的实现。

EnumMap on the other hand works the same for all enum types (it keeps an array of all enum items and an equally-sized array of values), so there's no need for an abstract class. 另一方面, EnumMap对所有枚举类型的作用相同(它保存所有枚举项的数组和大小相等的值),因此不需要抽象类。

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

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