Top Description Inners Fields Constructors Methods
java.util

public Class EnumMap<K extends Enum<K>, V>

extends AbstractMap<K extends Enum<K>, V>
implements Serializable, Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable, java.io.Serializable
Type Parameters
<K>
the enum type of keys maintained by this map
<V>
the type of mapped values
Imports
jdk.internal.access.SharedSecrets

A specialized 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.

Author
Josh Bloch
Since
1.5
See Also
EnumSet

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class
private abstract class
private class
private class
private class
private class

Field Summary

Modifier and TypeField and Description
private transient Set<Map.Entry<K, V>>
entrySet

This field is initialized to contain an instance of the entry set view the first time this view is requested.

private final Class<K>
keyType

The Class object for the enum type of all the keys of this map.

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.

Inherited from java.util.AbstractMap:
keySetvalues

Constructor Summary

AccessConstructor and Description
public
EnumMap(Class<K>
the class object of the key type for this enum map
keyType
)

Creates an empty enum map with the specified key type.

public
EnumMap(EnumMap<K, ? extends V>
the enum map from which to initialize this enum map
m
)

Creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).

public
EnumMap(Map<K, ? extends V>
the map from which to initialize this enum map
m
)

Creates an enum map initialized from the specified map.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Overrides java.util.AbstractMap.clear.

Implements java.util.Map.clear.

Removes all mappings from this map.

public EnumMap<K, V>

Returns:

a shallow copy of this enum map
clone
()

Overrides java.util.AbstractMap.clone.

Returns a shallow copy of this enum map.

public boolean

Returns:

true if this map contains a mapping for the specified key
containsKey
(Object
the key whose presence in this map is to be tested
key
)

Overrides java.util.AbstractMap.containsKey.

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.

private boolean
public boolean

Returns:

true if this map maps one or more keys to this value
containsValue
(Object
the value whose presence in this map is to be tested
value
)

Overrides java.util.AbstractMap.containsValue.

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.

private int
entryHashCode(int index)

public Set<Map.Entry<K, V>>

Returns:

a set view of the mappings contained in this enum map
entrySet
()

Implements abstract java.util.AbstractMap.entrySet.

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.

public boolean

Returns:

true if the specified object is equal to this map
equals
(Object
the object to be compared for equality with this map
o
)

Overrides java.util.AbstractMap.equals.

Implements java.util.Map.equals.

Compares the specified object with this map for equality.

private boolean
equals(EnumMap<?, ?> em)

public V
get(Object
the key whose associated value is to be returned
key
)

Overrides java.util.AbstractMap.get.

Implements java.util.Map.get.

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

private static <K extends Enum<K>> K[]
getKeyUniverse(Class<K> keyType)

Returns all of the values comprising K.

public int
hashCode()

Overrides java.util.AbstractMap.hashCode.

Implements java.util.Map.hashCode.

Returns the hash code value for this map.

private boolean
isValidKey(Object key)

Returns true if key is of the proper type to be a key in this enum map.

public Set<K>

Returns:

a set view of the keys contained in this enum map
keySet
()

Overrides java.util.AbstractMap.keySet.

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.

private Object
public V

Returns:

the previous value associated with specified key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with the specified key.)
put
(K
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.util.AbstractMap.put.

Implements java.util.Map.put.

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.util.AbstractMap.putAll.

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this map.

private void
readObject(ObjectInputStream s)

Reconstitute the EnumMap instance from a stream (i.e., deserialize it).

public V

Returns:

the previous value associated with specified key, or null if there was no entry for key. (A null return can also indicate that the map previously associated null with the specified key.)
remove
(Object
the key whose mapping is to be removed from the map
key
)

Overrides java.util.AbstractMap.remove.

Implements java.util.Map.remove.

Removes the mapping for this key from this map if present.

private boolean
public int

Returns:

the number of key-value mappings in this map
size
()

Overrides java.util.AbstractMap.size.

Implements java.util.Map.size.

Returns the number of key-value mappings in this map.

private void
typeCheck(K key)

Throws an exception if e is not of the correct type for this enum set.

private V
public Collection<V>

Returns:

a collection view of the values contained in this map
values
()

Overrides java.util.AbstractMap.values.

Implements java.util.Map.values.

Returns a Collection view of the values contained in this map.

private void
writeObject(ObjectOutputStream s)

Save the state of the EnumMap instance to a stream (i.e., serialize it).

Inherited from java.util.AbstractMap:
isEmptytoString