java.util
Class EnumMap<K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>

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

public class EnumMap<K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
extends AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K,V>
implements Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

A specialized Map sample code for java.util.Map definition code for java.util.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() sample code for java.util.EnumMap.keySet() definition code for java.util.EnumMap.keySet() , entrySet() sample code for java.util.EnumMap.entrySet() definition code for java.util.EnumMap.entrySet() , and values() sample code for java.util.EnumMap.values() definition code for java.util.EnumMap.values() ).

Iterators returned by the collection views are weakly consistent: they will never throw ConcurrentModificationException sample code for java.util.ConcurrentModificationException definition code for java.util.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 sample code for java.lang.NullPointerException definition code for java.lang.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(java.util.Map) sample code for java.util.Collections.synchronizedMap(java.util.Map) definition code for java.util.Collections.synchronizedMap(java.util.Map) method. This is best done at creation time, to prevent accidental unsynchronized access:

     Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap(...));
 

Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap sample code for java.util.HashMap definition code for java.util.HashMap counterparts.

This class is a member of the Java Collections Framework.

Since:
1.5
See Also:
EnumSet sample code for java.util.EnumSet definition code for java.util.EnumSet , Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map sample code for java.util.Map definition code for java.util.Map
Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>
 
Constructor Summary
EnumMap sample code for java.util.EnumMap.EnumMap(java.lang.Class) definition code for java.util.EnumMap.EnumMap(java.lang.Class) (Class sample code for java.lang.Class definition code for java.lang.Class <K> keyType)
          Creates an empty enum map with the specified key type.
EnumMap sample code for java.util.EnumMap.EnumMap(java.util.EnumMap) definition code for java.util.EnumMap.EnumMap(java.util.EnumMap) (EnumMap sample code for java.util.EnumMap definition code for java.util.EnumMap <K,? extends V> m)
          Creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).
EnumMap sample code for java.util.EnumMap.EnumMap(java.util.Map) definition code for java.util.EnumMap.EnumMap(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <K,? extends V> m)
          Creates an enum map initialized from the specified map.
 
Method Summary
 void clear sample code for java.util.EnumMap.clear() definition code for java.util.EnumMap.clear() ()
          Removes all mappings from this map.
 EnumMap sample code for java.util.EnumMap definition code for java.util.EnumMap <K,V> clone sample code for java.util.EnumMap.clone() definition code for java.util.EnumMap.clone() ()
          Returns a shallow copy of this enum map.
 boolean containsKey sample code for java.util.EnumMap.containsKey(java.lang.Object) definition code for java.util.EnumMap.containsKey(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue sample code for java.util.EnumMap.containsValue(java.lang.Object) definition code for java.util.EnumMap.containsValue(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  value)
          Returns true if this map maps one or more keys to the specified value.
 Set sample code for java.util.Set definition code for java.util.Set <Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>> entrySet sample code for java.util.EnumMap.entrySet() definition code for java.util.EnumMap.entrySet() ()
          Returns a Set sample code for java.util.Set definition code for java.util.Set view of the mappings contained in this map.
 boolean equals sample code for java.util.EnumMap.equals(java.lang.Object) definition code for java.util.EnumMap.equals(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  o)
          Compares the specified object with this map for equality.
 V get sample code for java.util.EnumMap.get(java.lang.Object) definition code for java.util.EnumMap.get(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Returns the value to which this map maps the specified key, or null if this map contains no mapping for the specified key.
 Set sample code for java.util.Set definition code for java.util.Set <K> keySet sample code for java.util.EnumMap.keySet() definition code for java.util.EnumMap.keySet() ()
          Returns a Set sample code for java.util.Set definition code for java.util.Set view of the keys contained in this map.
 V put sample code for java.util.EnumMap.put(K, V) definition code for java.util.EnumMap.put(K, V) (K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll sample code for java.util.EnumMap.putAll(java.util.Map) definition code for java.util.EnumMap.putAll(java.util.Map) (Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
          Copies all of the mappings from the specified map to this map.
 V remove sample code for java.util.EnumMap.remove(java.lang.Object) definition code for java.util.EnumMap.remove(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  key)
          Removes the mapping for this key from this map if present.
 int size sample code for java.util.EnumMap.size() definition code for java.util.EnumMap.size() ()
          Returns the number of key-value mappings in this map.
 Collection sample code for java.util.Collection definition code for java.util.Collection <V> values sample code for java.util.EnumMap.values() definition code for java.util.EnumMap.values() ()
          Returns a Collection sample code for java.util.Collection definition code for java.util.Collection view of the values contained in this map.
 
Methods inherited from class java.util.AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap
hashCode sample code for java.util.AbstractMap.hashCode() definition code for java.util.AbstractMap.hashCode() , isEmpty sample code for java.util.AbstractMap.isEmpty() definition code for java.util.AbstractMap.isEmpty() , toString sample code for java.util.AbstractMap.toString() definition code for java.util.AbstractMap.toString()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

EnumMap sample code for java.util.EnumMap(java.lang.Class<K>) definition code for java.util.EnumMap(java.lang.Class<K>)

public EnumMap(Class sample code for java.lang.Class definition code for java.lang.Class <K> keyType)
Creates an empty enum map with the specified key type.

Parameters:
keyType - the class object of the key type for this enum map
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if keyType is null

EnumMap sample code for java.util.EnumMap(java.util.EnumMap<K, ? extends V>) definition code for java.util.EnumMap(java.util.EnumMap<K, ? extends V>)

public EnumMap(EnumMap sample code for java.util.EnumMap definition code for java.util.EnumMap <K,? extends V> m)
Creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).

Parameters:
m - the enum map from which to initialize this enum map
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if m is null

EnumMap sample code for java.util.EnumMap(java.util.Map<K, ? extends V>) definition code for java.util.EnumMap(java.util.Map<K, ? extends V>)

public EnumMap(Map sample code for java.util.Map definition code for java.util.Map <K,? extends V> m)
Creates an enum map initialized from the specified map. If the specified map is an EnumMap instance, this constructor behaves identically to EnumMap(EnumMap) sample code for java.util.EnumMap.EnumMap(java.util.EnumMap) definition code for java.util.EnumMap.EnumMap(java.util.EnumMap) . Otherwise, the specified map must contain at least one mapping (in order to determine the new enum map's key type).

Parameters:
m - the map from which to initialize this enum map
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if m is not an EnumMap instance and contains no mappings
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if m is null
Method Detail

size sample code for java.util.EnumMap.size() definition code for java.util.EnumMap.size()

public int size()
Returns the number of key-value mappings in this map.

Specified by:
size sample code for java.util.Map.size() definition code for java.util.Map.size() in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
size sample code for java.util.AbstractMap.size() definition code for java.util.AbstractMap.size() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Returns:
the number of key-value mappings in this map

containsValue sample code for java.util.EnumMap.containsValue(java.lang.Object) definition code for java.util.EnumMap.containsValue(java.lang.Object)

public boolean containsValue(Object sample code for java.lang.Object definition code for java.lang.Object  value)
Returns true if this map maps one or more keys to the specified value.

Specified by:
containsValue sample code for java.util.Map.containsValue(java.lang.Object) definition code for java.util.Map.containsValue(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
containsValue sample code for java.util.AbstractMap.containsValue(java.lang.Object) definition code for java.util.AbstractMap.containsValue(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
value - the value whose presence in this map is to be tested
Returns:
true if this map maps one or more keys to this value

containsKey sample code for java.util.EnumMap.containsKey(java.lang.Object) definition code for java.util.EnumMap.containsKey(java.lang.Object)

public boolean containsKey(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey sample code for java.util.Map.containsKey(java.lang.Object) definition code for java.util.Map.containsKey(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
containsKey sample code for java.util.AbstractMap.containsKey(java.lang.Object) definition code for java.util.AbstractMap.containsKey(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
key - the key whose presence in this map is to be tested
Returns:
true if this map contains a mapping for the specified key

get sample code for java.util.EnumMap.get(java.lang.Object) definition code for java.util.EnumMap.get(java.lang.Object)

public V get(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Returns the value to which this map maps the specified key, or null if this map contains no mapping for the specified key.

Specified by:
get sample code for java.util.Map.get(java.lang.Object) definition code for java.util.Map.get(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
get sample code for java.util.AbstractMap.get(java.lang.Object) definition code for java.util.AbstractMap.get(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
key - the key whose associated value is to be returned
Returns:
the value to which this map maps the specified key, or null if this map contains no mapping for the specified key
See Also:
AbstractMap.containsKey(Object) sample code for java.util.AbstractMap.containsKey(java.lang.Object) definition code for java.util.AbstractMap.containsKey(java.lang.Object)

put sample code for java.util.EnumMap.put(K, V) definition code for java.util.EnumMap.put(K, V)

public V put(K key,
             V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put sample code for java.util.Map.put(K, V) definition code for java.util.Map.put(K, V) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
put sample code for java.util.AbstractMap.put(K, V) definition code for java.util.AbstractMap.put(K, V) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
key - the key with which the specified value is to be associated
value - the value to be associated with the specified key
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.)
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if the specified key is null

remove sample code for java.util.EnumMap.remove(java.lang.Object) definition code for java.util.EnumMap.remove(java.lang.Object)

public V remove(Object sample code for java.lang.Object definition code for java.lang.Object  key)
Removes the mapping for this key from this map if present.

Specified by:
remove sample code for java.util.Map.remove(java.lang.Object) definition code for java.util.Map.remove(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
remove sample code for java.util.AbstractMap.remove(java.lang.Object) definition code for java.util.AbstractMap.remove(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
key - the key whose mapping is to be removed from the map
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.)

putAll sample code for java.util.EnumMap.putAll(java.util.Map<? extends K, ? extends V>) definition code for java.util.EnumMap.putAll(java.util.Map<? extends K, ? extends V>)

public void putAll(Map sample code for java.util.Map definition code for java.util.Map <? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Specified by:
putAll sample code for java.util.Map.putAll(java.util.Map) definition code for java.util.Map.putAll(java.util.Map) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
putAll sample code for java.util.AbstractMap.putAll(java.util.Map) definition code for java.util.AbstractMap.putAll(java.util.Map) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
m - the mappings to be stored in this map
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - the specified map is null, or if one or more keys in the specified map are null

clear sample code for java.util.EnumMap.clear() definition code for java.util.EnumMap.clear()

public void clear()
Removes all mappings from this map.

Specified by:
clear sample code for java.util.Map.clear() definition code for java.util.Map.clear() in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
clear sample code for java.util.AbstractMap.clear() definition code for java.util.AbstractMap.clear() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>

keySet sample code for java.util.EnumMap.keySet() definition code for java.util.EnumMap.keySet()

public Set sample code for java.util.Set definition code for java.util.Set <K> keySet()
Returns a Set sample code for java.util.Set definition code for java.util.Set view of the keys contained in this map. The returned set obeys the general contract outlined in Map.keySet() sample code for java.util.Map.keySet() definition code for java.util.Map.keySet() . The set's iterator will return the keys in their natural order (the order in which the enum constants are declared).

Specified by:
keySet sample code for java.util.Map.keySet() definition code for java.util.Map.keySet() in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
keySet sample code for java.util.AbstractMap.keySet() definition code for java.util.AbstractMap.keySet() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Returns:
a set view of the keys contained in this enum map

values sample code for java.util.EnumMap.values() definition code for java.util.EnumMap.values()

public Collection sample code for java.util.Collection definition code for java.util.Collection <V> values()
Returns a Collection sample code for java.util.Collection definition code for java.util.Collection view of the values contained in this map. The returned collection obeys the general contract outlined in Map.values() sample code for java.util.Map.values() definition code for java.util.Map.values() . The collection's iterator will return the values in the order their corresponding keys appear in map, which is their natural order (the order in which the enum constants are declared).

Specified by:
values sample code for java.util.Map.values() definition code for java.util.Map.values() in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
values sample code for java.util.AbstractMap.values() definition code for java.util.AbstractMap.values() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Returns:
a collection view of the values contained in this map

entrySet sample code for java.util.EnumMap.entrySet() definition code for java.util.EnumMap.entrySet()

public Set sample code for java.util.Set definition code for java.util.Set <Map.Entry sample code for java.util.Map.Entry definition code for java.util.Map.Entry <K,V>> entrySet()
Returns a Set sample code for java.util.Set definition code for java.util.Set view of the mappings contained in this map. The returned set obeys the general contract outlined in Map.keySet() sample code for java.util.Map.keySet() definition code for java.util.Map.keySet() . The set's iterator will return the mappings in the order their keys appear in map, which is their natural order (the order in which the enum constants are declared).

Specified by:
entrySet sample code for java.util.Map.entrySet() definition code for java.util.Map.entrySet() in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Specified by:
entrySet sample code for java.util.AbstractMap.entrySet() definition code for java.util.AbstractMap.entrySet() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Returns:
a set view of the mappings contained in this enum map

equals sample code for java.util.EnumMap.equals(java.lang.Object) definition code for java.util.EnumMap.equals(java.lang.Object)

public boolean equals(Object sample code for java.lang.Object definition code for java.lang.Object  o)
Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings, as specified in the Map.equals(Object) sample code for java.util.Map.equals(java.lang.Object) definition code for java.util.Map.equals(java.lang.Object) contract.

Specified by:
equals sample code for java.util.Map.equals(java.lang.Object) definition code for java.util.Map.equals(java.lang.Object) in interface Map sample code for java.util.Map definition code for java.util.Map <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Overrides:
equals sample code for java.util.AbstractMap.equals(java.lang.Object) definition code for java.util.AbstractMap.equals(java.lang.Object) in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Parameters:
o - the object to be compared for equality with this map
Returns:
true if the specified object is equal to this map
See Also:
Object.hashCode() sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , Hashtable sample code for java.util.Hashtable definition code for java.util.Hashtable

clone sample code for java.util.EnumMap.clone() definition code for java.util.EnumMap.clone()

public EnumMap sample code for java.util.EnumMap definition code for java.util.EnumMap <K,V> clone()
Returns a shallow copy of this enum map. (The values themselves are not cloned.

Overrides:
clone sample code for java.util.AbstractMap.clone() definition code for java.util.AbstractMap.clone() in class AbstractMap sample code for java.util.AbstractMap definition code for java.util.AbstractMap <K extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <K>,V>
Returns:
a shallow copy of this enum map
See Also:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable