java.lang.Object
java.util.AbstractMap<K,V>
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 Serializable, Cloneable, Map<K,V>
An
Map specialized for use with Enum types as keys.- See Also:
- Serialized Form
-
Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> -
Constructor Summary
Constructors Constructor Description EnumMap(Class<K> keyType)Constructs an emptyEnumMapusing the given key type.EnumMap(EnumMap<K,? extends V> map)Constructs anEnumMapusing the same key type as the givenEnumMapand initially containing the same mappings.EnumMap(Map<K,? extends V> map)Constructs anEnumMapinitialized from the given map. -
Method Summary
Modifier and Type Method Description voidclear()Removes all elements from thisEnumMap, leaving it empty.EnumMap<K,V>clone()Returns a shallow copy of thisEnumMap.booleancontainsKey(Object key)Returns whether thisEnumMapcontains the specified key.booleancontainsValue(Object value)Returns whether thisEnumMapcontains the specified value.Set<Map.Entry<K,V>>entrySet()Returns aSetcontaining all of the mappings in thisEnumMap.booleanequals(Object object)Compares the argument to the receiver, and returnstrueif the specifiedObjectis anEnumMapand bothEnumMaps contain the same mappings.Vget(Object key)Returns the value of the mapping with the specified key.Set<K>keySet()Returns a set of the keys contained in thisEnumMap.Vput(K key, V value)Maps the specified key to the specified value.voidputAll(Map<? extends K,? extends V> map)Copies every mapping in the specifiedMapto thisEnumMap.Vremove(Object key)Removes a mapping with the specified key from thisEnumMap.intsize()Returns the number of elements in thisEnumMap.Collection<V>values()Returns aCollectionof the values contained in thisEnumMap.Methods inherited from class java.util.AbstractMap
hashCode, isEmpty, toString
-
Constructor Details
-
EnumMap
Constructs an emptyEnumMapusing the given key type.- Parameters:
keyType- the class object giving the type of the keys used by thisEnumMap.- Throws:
NullPointerException- ifkeyTypeisnull.
-
EnumMap
Constructs anEnumMapusing the same key type as the givenEnumMapand initially containing the same mappings.- Parameters:
map- theEnumMapfrom which thisEnumMapis initialized.- Throws:
NullPointerException- ifmapisnull.
-
EnumMap
Constructs anEnumMapinitialized from the given map. If the given map is anEnumMapinstance, this constructor behaves in the exactly the same way asEnumMap(EnumMap)}. Otherwise, the given map should contain at least one mapping.- Parameters:
map- the map from which thisEnumMapis initialized.- Throws:
IllegalArgumentException- ifmapis not anEnumMapinstance and does not contain any mappings.NullPointerException- ifmapisnull.
-
-
Method Details
-
clear
public void clear()Removes all elements from thisEnumMap, leaving it empty. -
clone
Returns a shallow copy of thisEnumMap. -
containsKey
Returns whether thisEnumMapcontains the specified key.- Specified by:
containsKeyin interfaceMap<K extends Enum<K>,V>- Overrides:
containsKeyin classAbstractMap<K extends Enum<K>,V>- Parameters:
key- the key to search for.- Returns:
trueif thisEnumMapcontains the specified key,falseotherwise.
-
containsValue
Returns whether thisEnumMapcontains the specified value.- Specified by:
containsValuein interfaceMap<K extends Enum<K>,V>- Overrides:
containsValuein classAbstractMap<K extends Enum<K>,V>- Parameters:
value- the value to search for.- Returns:
trueif thisEnumMapcontains the specified value,falseotherwise.
-
entrySet
Returns aSetcontaining all of the mappings in thisEnumMap. Each mapping is an instance ofMap.Entry. As theSetis backed by thisEnumMap, changes in one will be reflected in the other.The order of the entries in the set will be the order that the enum keys were declared in.
-
equals
Compares the argument to the receiver, and returnstrueif the specifiedObjectis anEnumMapand bothEnumMaps contain the same mappings.- Specified by:
equalsin interfaceMap<K extends Enum<K>,V>- Overrides:
equalsin classAbstractMap<K extends Enum<K>,V>- Parameters:
object- theObjectto compare with thisEnumMap.- Returns:
- boolean
trueifobjectis the same as thisEnumMap,falseotherwise. - See Also:
AbstractMap.hashCode(),entrySet()
-
get
Returns the value of the mapping with the specified key. -
keySet
Returns a set of the keys contained in thisEnumMap. TheSetis backed by thisEnumMapso changes to one are reflected in the other. TheSetdoes not support adding.The order of the set will be the order that the enum keys were declared in.
-
put
Maps the specified key to the specified value.- Specified by:
putin interfaceMap<K extends Enum<K>,V>- Overrides:
putin classAbstractMap<K extends Enum<K>,V>- Parameters:
key- the key.value- the value.- Returns:
- the value of any previous mapping with the specified key or
nullif there was no mapping. - Throws:
UnsupportedOperationException- if adding to this map is not supported.ClassCastException- if the class of the key or value is inappropriate for this map.IllegalArgumentException- if the key or value cannot be added to this map.NullPointerException- if the key or value isnulland thisEnumMapdoes not supportnullkeys or values.
-
putAll
Copies every mapping in the specifiedMapto thisEnumMap.- Specified by:
putAllin interfaceMap<K extends Enum<K>,V>- Overrides:
putAllin classAbstractMap<K extends Enum<K>,V>- Parameters:
map- theMapto copy mappings from.- Throws:
UnsupportedOperationException- if adding to thisEnumMapis not supported.ClassCastException- if the class of a key or value is inappropriate for thisEnumMap.IllegalArgumentException- if a key or value cannot be added to this map.NullPointerException- if a key or value isnulland thisEnumMapdoes not supportnullkeys or values.
-
remove
Removes a mapping with the specified key from thisEnumMap.- Specified by:
removein interfaceMap<K extends Enum<K>,V>- Overrides:
removein classAbstractMap<K extends Enum<K>,V>- Parameters:
key- the key of the mapping to remove.- Returns:
- the value of the removed mapping or
nullif no mapping for the specified key was found. - Throws:
UnsupportedOperationException- if removing from thisEnumMapis not supported.
-
size
public int size()Returns the number of elements in thisEnumMap. -
values
Returns aCollectionof the values contained in thisEnumMap. The returnedCollectioncomplies with the general rule specified inMap.values(). TheCollection'sIteratorwill return the values in the their corresponding keys' natural order (theEnumconstants are declared in this order).The order of the values in the collection will be the order that their corresponding enum keys were declared in.
-