I was wondering, is there any reason why EnumSet is implemented as abstract class and EnumMap is implemented as concrete class?
EnumMap is a specialized implementation of the Map interface for the enumeration types. EnumSet is a specialized implementation of the Set interface for the enumeration types. EnumMap is internally represented as an array. EnumSet is internally represented as a BitVector.
The EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type. A few important features of EnumSet are as follows: It extends AbstractSet class and implements Set Interface in Java. EnumSet class is a member of the Java Collections Framework & is not synchronized.
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[]
). Factory methods of EnumSet
return one of them depending on the enum class passed in.
Such an optimization doesn't make sense for EnumMap
(since array to store values is needed anyways), therefore EnumMap
is a concrete class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With