Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why EnumSet is implemented as abstract class and EnumMap is implemented as concrete class?

Tags:

I was wondering, is there any reason why EnumSet is implemented as abstract class and EnumMap is implemented as concrete class?

like image 963
Cheok Yan Cheng Avatar asked Mar 03 '11 14:03

Cheok Yan Cheng


People also ask

What is EnumSet and EnumMap in Java?

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.

What is the use of EnumSet?

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.


1 Answers

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.

like image 148
axtavt Avatar answered Sep 19 '22 12:09

axtavt