Map
implementation for use with enum type keys. All
of the keys in an enum map must come from a single enum type that is
specified, explicitly or implicitly, when the map is created. Enum maps
are represented internally as arrays. This representation is extremely
compact and efficient.
Enum maps are maintained in the natural order of their keys
(the order in which the enum constants are declared). This is reflected
in the iterators returned by the collections views (keySet()
,
entrySet()
, and values()
).
Iterators returned by the collection views are weakly consistent:
they will never throw ConcurrentModificationException
and they may
or may not show the effects of any modifications to the map that occur while
the iteration is in progress.
Null keys are not permitted. Attempts to insert a null key will
throw NullPointerException
. Attempts to test for the
presence of a null key or to remove one will, however, function properly.
Null values are permitted.
Like most collection implementations EnumMap
is not
synchronized. If multiple threads access an enum map concurrently, and at
least one of the threads modifies the map, it should be synchronized
externally. This is typically accomplished by synchronizing on some
object that naturally encapsulates the enum map. If no such object exists,
the map should be "wrapped" using the Collections#synchronizedMap
method. This is best done at creation time, to prevent accidental
unsynchronized access:
Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));
Implementation Note
All basic operations execute in constant time.
They are likely (though not guaranteed) to be faster than their
HashMap
counterparts.
This class is a member of the Java Collections Framework.
EnumSet
Modifier and Type | Class and Description |
---|---|
private class | |
private class | |
private abstract class | |
private class | |
private class | |
private class | |
private class |
Modifier and Type | Field and Description |
---|---|
private transient Set | entrySet
This field is initialized to contain an instance of the entry set view the first time this view is requested. |
private final Class | keyType
The |
private transient K[] | keyUniverse
All of the values comprising K. |
private static final Object | NULL
Distinguished non-null value for representing null values. |
private static final long | |
private transient int | size
The number of mappings in this map. |
private transient Object[] | vals
Array representation of this map. |
Access | Constructor and Description |
---|---|
public | |
public | |
public |
Modifier and Type | Method and Description |
---|---|
public void | clear()
Overrides java. Implements java. Removes all mappings from this map. |
public EnumMap | Returns: a shallow copy of this enum mapOverrides java. Returns a shallow copy of this enum map. |
public boolean | Returns: true if this map contains a mapping for the specified
keythe key whose presence in this map is to be tested key)Overrides java. Implements java. Returns |
private boolean | |
public boolean | Returns: true if this map maps one or more keys to this valuethe value whose presence in this map is to be tested value)Overrides java. Implements java. Returns |
private int | |
public Set | Returns: a set view of the mappings contained in this enum mapImplements abstract java. Implements java. Returns a |
public boolean | Returns: true if the specified object is equal to this mapthe object to be compared for equality with this map o)Overrides java. Implements java. Compares the specified object with this map for equality. |
private boolean | |
public V | get(Object
the key whose associated value is to be returned key)Overrides java. Implements java. Returns the value to which the specified key is mapped,
or |
private static <K extends Enum | |
public int | hashCode()
Overrides java. Implements java. Returns the hash code value for this map. |
private boolean | |
public Set | Returns: a set view of the keys contained in this enum mapOverrides java. Implements java. Returns a |
private Object | |
public V | Returns: the previous value associated with specified key, ornull if there was no mapping for key. (A null
return can also indicate that the map previously associated
null with the specified key.)the key with which the specified value is to be associated key, V the value to be associated with the specified key value)Overrides java. Implements java. Associates the specified value with the specified key in this map. |
public void | putAll(Map<? extends K, ? extends V>
the mappings to be stored in this map m)Overrides java. Implements java. Copies all of the mappings from the specified map to this map. |
private void | readObject(ObjectInputStream s)
Reconstitute the |
public V | Returns: the previous value associated with specified key, ornull if there was no entry for key. (A null
return can also indicate that the map previously associated
null with the specified key.)the key whose mapping is to be removed from the map key)Overrides java. Implements java. Removes the mapping for this key from this map if present. |
private boolean | |
public int | Returns: the number of key-value mappings in this mapOverrides java. Implements java. Returns the number of key-value mappings in this map. |
private void | |
private V | |
public Collection | Returns: a collection view of the values contained in this mapOverrides java. Implements java. Returns a |
private void | writeObject(ObjectOutputStream s)
Save the state of the |