This article introduces
EnumSet, which is a collection class under the
Set structures are especially used in our daily work, and often used to store data or pass parameters, but there is no way to control the data setting of
Map, we have no idea what kind of data others will
put into it, or if the type and number of data
Key of our
Map is fixed in some scenarios, then in this case How can we improve the security and performance of the system?
This time we can consider using
EnumMap is a
Map, and secondly its
key can only be an enumeration, we all know that the number of instances in the enumeration is fixed, and still pre-compiled, so to a large extent to ensure the security of the data, but also to improve the performance of certain.
Let’s look at how to use
EnumMap, first we need to create an enumeration
After creating a test class
We can see that we need to pass in an enum class when constructing
EnumMap, and the subsequent
get are the same as
Map, except that this time the
key must be the enum instance when
put is used. Next, let’s look at how the
get methods of
EnumMap are implemented, and looking at the
JDK source code we can see that
put is done, type checking is done first and if the passed in enum is not an enum or not the enum specified during construction, an exception will be thrown here. When the type check is passed, the index of the enumeration instance is obtained by the
ordinal() method of the enumeration, which returns an
int value, and the returned value is related to the order of the enumeration when it is written. RED.ordinal()
will return 1. Once the index is obtained, thevalue` value will be placed in the corresponding array position.
It’s easy to get the data, just get the index directly from
key and then remove the data from the array.
You can see that the efficiency of
get of the whole
EnumMap is very high, both are directly directed to process based on index in a one-dimensional array. So you can try to use this approach to improve performance in similar scenarios in the future.
Let’s take a look at
EnumSet is a collection used to manipulate
Enum, an abstract class that has two inherited classes,
RegularEnumSet. To use it, you need to determine the enumeration type. An empty
EnumSet can be created and used later by the following way.
The construction of
EnumSet is a bit more involved, we can create empty sets, and we can also create a complete set directly, there is no need to create an empty one and then do the
add operation, as follows
In addition, as mentioned earlier, the
ordinal() method of enumeration is used, so when constructing an
EnumSet we can also construct only all the enumeration values between the specified two enumeration ranges, and note here that the enumeration of the second argument of the
range method cannot precede the first enumeration.
You can also use the
of method of
EnumSet to construct the specified enumeration set. We can find out from the source code that no matter what method is used to construct it, the bottom is to construct an empty set first and then add the corresponding enumeration elements. The logic for constructing an empty set is as follows. Here we can see that when the number of enumerations is greater than 64, the subclass
JumboEnumSet is used, otherwise it is the subclass
The other methods are the same as for a regular Set, and you can use the set in your work exactly as it is.
Today we introduced you to two uncommon, but very useful
JDK set classes that can still improve performance in some scenarios.