java.util
Class EnumMap<K extends Enum<K>,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.EnumMap<K,V>
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>

public class EnumMap<K extends Enum<K>,V>
extends AbstractMap<K,V>
implements Cloneable, Serializable

Since:
1.5
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
EnumMap(Class<K> keyType)
           
EnumMap(EnumMap<K,? extends V> map)
           
EnumMap(Map<K,? extends V> map)
           
 
Method Summary
 void clear()
          Remove all entries from this Map (optional operation).
 EnumMap<K,V> clone()
          Create a shallow copy of this Map, no keys or values are copied.
 boolean containsKey(Object key)
          Returns true if this contains a mapping for the given key.
 boolean containsValue(Object value)
          Returns true if this contains at least one mapping with the given value.
 Set<Map.Entry<K,V>> entrySet()
          Returns a set view of the mappings in this Map.
 boolean equals(Object o)
          Compares the specified object with this map for equality.
 V get(Object key)
          Returns the value mapped by the given key.
 Set<K> keySet()
          Returns a set view of this map's keys.
 V put(K key, V value)
          Associates the given key to the given value (optional operation).
 void putAll(Map<? extends K,? extends V> map)
          Copies all entries of the given map to this one (optional operation).
 V remove(Object key)
          Removes the mapping for this key if present (optional operation).
 int size()
          Returns the number of key-value mappings in the map.
 Collection<V> values()
          Returns a collection or bag view of this map's values.
 
Methods inherited from class java.util.AbstractMap
hashCode, isEmpty, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EnumMap

public EnumMap(Class<K> keyType)

EnumMap

public EnumMap(EnumMap<K,? extends V> map)

EnumMap

public EnumMap(Map<K,? extends V> map)
Method Detail

size

public int size()
Description copied from class: AbstractMap
Returns the number of key-value mappings in the map. If there are more than Integer.MAX_VALUE mappings, return Integer.MAX_VALUE. This is implemented as entrySet().size().

Specified by:
size in interface Map<K extends Enum<K>,V>
Overrides:
size in class AbstractMap<K extends Enum<K>,V>
Returns:
the number of mappings
See Also:
Set.size()

containsValue

public boolean containsValue(Object value)
Description copied from class: AbstractMap
Returns true if this contains at least one mapping with the given value. This implementation does a linear search, O(n), over the entrySet(), returning true if a match is found, false if the iteration ends. A match is defined as a value, v, where (value == null ? v == null : value.equals(v)). Subclasses are unlikely to implement this more efficiently.

Specified by:
containsValue in interface Map<K extends Enum<K>,V>
Overrides:
containsValue in class AbstractMap<K extends Enum<K>,V>
Parameters:
value - the value to search for
Returns:
true if the map contains the value
See Also:
AbstractMap.containsKey(Object)

containsKey

public boolean containsKey(Object key)
Description copied from class: AbstractMap
Returns true if this contains a mapping for the given key. This implementation does a linear search, O(n), over the entrySet(), returning true if a match is found, false if the iteration ends. Many subclasses can implement this more efficiently.

Specified by:
containsKey in interface Map<K extends Enum<K>,V>
Overrides:
containsKey in class AbstractMap<K extends Enum<K>,V>
Parameters:
key - the key to search for
Returns:
true if the map contains the key
See Also:
AbstractMap.containsValue(Object)

get

public V get(Object key)
Description copied from class: AbstractMap
Returns the value mapped by the given key. Returns null if there is no mapping. However, in Maps that accept null values, you must rely on containsKey to determine if a mapping exists. This iteration takes linear time, searching entrySet().iterator() of the key. Many implementations override this method.

Specified by:
get in interface Map<K extends Enum<K>,V>
Overrides:
get in class AbstractMap<K extends Enum<K>,V>
Parameters:
key - the key to look up
Returns:
the value associated with the key, or null if key not in map
See Also:
AbstractMap.containsKey(Object)

put

public V put(K key,
             V value)
Description copied from class: AbstractMap
Associates the given key to the given value (optional operation). If the map already contains the key, its value is replaced. This implementation simply throws an UnsupportedOperationException. Be aware that in a map that permits null values, a null return does not always imply that the mapping was created.

Specified by:
put in interface Map<K extends Enum<K>,V>
Overrides:
put in class AbstractMap<K extends Enum<K>,V>
Parameters:
key - the key to map
value - the value to be mapped
Returns:
the previous value of the key, or null if there was no mapping
See Also:
AbstractMap.containsKey(Object)

remove

public V remove(Object key)
Description copied from class: AbstractMap
Removes the mapping for this key if present (optional operation). This implementation iterates over the entrySet searching for a matching key, at which point it calls the iterator's remove method. It returns the result of getValue() on the entry, if found, or null if no entry is found. Note that maps which permit null values may also return null if the key was removed. If the entrySet does not support removal, this will also fail. This is O(n), so many implementations override it for efficiency.

Specified by:
remove in interface Map<K extends Enum<K>,V>
Overrides:
remove in class AbstractMap<K extends Enum<K>,V>
Parameters:
key - the key to remove
Returns:
the value the key mapped to, or null if not present. Null may also be returned if null values are allowed in the map and the value of this mapping is null.
See Also:
Iterator.remove()

putAll

public void putAll(Map<? extends K,? extends V> map)
Description copied from class: AbstractMap
Copies all entries of the given map to this one (optional operation). If the map already contains a key, its value is replaced. This implementation simply iterates over the map's entrySet(), calling put, so it is not supported if puts are not.

Specified by:
putAll in interface Map<K extends Enum<K>,V>
Overrides:
putAll in class AbstractMap<K extends Enum<K>,V>
Parameters:
map - the mapping to load into this map
See Also:
AbstractMap.put(Object, Object)

clear

public void clear()
Description copied from class: AbstractMap
Remove all entries from this Map (optional operation). This default implementation calls entrySet().clear(). NOTE: If the entry set does not permit clearing, then this will fail, too. Subclasses often override this for efficiency. Your implementation of entrySet() should not call AbstractMap.clear unless you want an infinite loop.

Specified by:
clear in interface Map<K extends Enum<K>,V>
Overrides:
clear in class AbstractMap<K extends Enum<K>,V>
See Also:
Set.clear()

keySet

public Set<K> keySet()
Description copied from class: AbstractMap
Returns a set view of this map's keys. The set is backed by the map, so changes in one show up in the other. Modifications while an iteration is in progress produce undefined behavior. The set supports removal if entrySet() does, but does not support element addition.

This implementation creates an AbstractSet, where the iterator wraps the entrySet iterator, size defers to the Map's size, and contains defers to the Map's containsKey. The set is created on first use, and returned on subsequent uses, although since no synchronization occurs, there is a slight possibility of creating two sets.

Specified by:
keySet in interface Map<K extends Enum<K>,V>
Overrides:
keySet in class AbstractMap<K extends Enum<K>,V>
Returns:
a Set view of the keys
See Also:
Set.iterator(), AbstractMap.size(), AbstractMap.containsKey(Object), AbstractMap.values()

values

public Collection<V> values()
Description copied from class: AbstractMap
Returns a collection or bag view of this map's values. The collection is backed by the map, so changes in one show up in the other. Modifications while an iteration is in progress produce undefined behavior. The collection supports removal if entrySet() does, but does not support element addition.

This implementation creates an AbstractCollection, where the iterator wraps the entrySet iterator, size defers to the Map's size, and contains defers to the Map's containsValue. The collection is created on first use, and returned on subsequent uses, although since no synchronization occurs, there is a slight possibility of creating two collections.

Specified by:
values in interface Map<K extends Enum<K>,V>
Overrides:
values in class AbstractMap<K extends Enum<K>,V>
Returns:
a Collection view of the values
See Also:
Collection.iterator(), AbstractMap.size(), AbstractMap.containsValue(Object), AbstractMap.keySet()

entrySet

public Set<Map.Entry<K,V>> entrySet()
Description copied from class: AbstractMap
Returns a set view of the mappings in this Map. Each element in the set must be an implementation of Map.Entry. The set is backed by the map, so that changes in one show up in the other. Modifications made while an iterator is in progress cause undefined behavior. If the set supports removal, these methods must be valid: Iterator.remove, Set.remove, removeAll, retainAll, and clear. Element addition is not supported via this set.

Specified by:
entrySet in interface Map<K extends Enum<K>,V>
Specified by:
entrySet in class AbstractMap<K extends Enum<K>,V>
Returns:
the entry set
See Also:
Map.Entry

equals

public boolean equals(Object o)
Description copied from class: AbstractMap
Compares the specified object with this map for equality. Returns true if the other object is a Map with the same mappings, that is,
o instanceof Map && entrySet().equals(((Map) o).entrySet();

Specified by:
equals in interface Map<K extends Enum<K>,V>
Overrides:
equals in class AbstractMap<K extends Enum<K>,V>
Parameters:
o - the object to be compared
Returns:
true if the object equals this map
See Also:
Set.equals(Object)

clone

public EnumMap<K,V> clone()
Description copied from class: AbstractMap
Create a shallow copy of this Map, no keys or values are copied. The default implementation simply calls super.clone().

Overrides:
clone in class AbstractMap<K extends Enum<K>,V>
Returns:
the shallow clone
See Also:
Cloneable, Object.clone()