Container -- EnumMap, enummap
I. Overview
EnumMap is a special type of Map. Its special feature is that the KEY must be of the enumeration type. Because the enumeration type features that the number of values is fixed, for EnumMap, the number of data that can be stored is fixed. This type of Map is relatively simple.
Ii. Main Implementation
1. Initialization
Because of the enum feature of EnumMap, the container capacity remains unchanged. Therefore, when creating an EnumMap, We need to specify its size, currently, you can create an EnumMap in the following ways:
Public EnumMap (Class <K> keyType): based on the class type of the key, all possible values of this enumeration are obtained through reflection, and a key array and value array are generated.
Public EnumMap (EnumMap <K ,? Extends V> m): It is initialized from another EnumMap. In essence, a copy is generated.
Public EnumMap (Map <K ,? Extends V> m): initializes the Map based on another Map. Compared with the second method, the Map conversion process is added.
After the above three initialization methods, an array of keys and values will be created internally, and their sizes are the same. In particular, the key array can be reused because the key is fixed for the same class of enum.
2. Storage
Let's take a look at the implementation of put operations.
Public V put (K key, V value) {typeCheck (key); // type check int index = key. ordinal (); Object oldValue = vals [index]; vals [index] = maskNull (value); // null value processing if (oldValue = null) size ++; return unmaskNull (oldValue );}
The entire implementation is very simple. The value corresponding to the key is stored in the vals array, and the location of the base object corresponding to the ordinal of the key.
Note that the system processes the value maskNull during storage, and unmaskNull is returned. Next, let's take a look at the relevant code:
private static final Object NULL = new Object() { public int hashCode() { return 0; } public String toString() { return "java.util.EnumMap.NULL"; } }; private Object maskNull(Object value) { return (value == null ? NULL : value); } private V unmaskNull(Object value) { return (V) (value == NULL ? null : value); }
We can see that the function of this method is: if the target value is null, it is replaced by a NULL object. Why?
This is because there is no value in the vals array by default, which is expressed by null. Therefore, EnumMap itself supports null values. If vals is set to null without any processing, the values cannot be distinguished from those without values, therefore, this method is used to represent null values.
3. Values
Like the stored value, the value is also relatively simple, as shown below:
public V get(Object key) { return (isValidKey(key) ? unmaskNull(vals[((Enum)key).ordinal()]) : null); }
It can be seen that, compared with HashMap, this value operation is directly located and very fast.
4. key Existence judgment
As mentioned above, the array of keys is determined during initialization. Does it indicate that all keys exist? See Implementation:
public boolean containsKey(Object key) { return isValidKey(key) && vals[((Enum)key).ordinal()] != null; }
It can be seen that if the subscript corresponding to a key does not set a value, the system determines that the key is not included.
Iii. Summary
From the previous analysis, we can see that the implementation of EnumMap is very simple and the access is very efficient. If we can set several fixed key values in our business scenario, this type of key is very efficient.