Package java.util

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

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 empty EnumMap using the given key type.
    EnumMap​(EnumMap<K,​? extends V> map)
    Constructs an EnumMap using the same key type as the given EnumMap and initially containing the same mappings.
    EnumMap​(Map<K,​? extends V> map)
    Constructs an EnumMap initialized from the given map.
  • Method Summary

    Modifier and Type Method Description
    void clear()
    Removes all elements from this EnumMap, leaving it empty.
    EnumMap<K,​V> clone()
    Returns a shallow copy of this EnumMap.
    boolean containsKey​(Object key)
    Returns whether this EnumMap contains the specified key.
    boolean containsValue​(Object value)
    Returns whether this EnumMap contains the specified value.
    Set<Map.Entry<K,​V>> entrySet()
    Returns a Set containing all of the mappings in this EnumMap.
    boolean equals​(Object object)
    Compares the argument to the receiver, and returns true if the specified Object is an EnumMap and both EnumMaps contain the same mappings.
    V get​(Object key)
    Returns the value of the mapping with the specified key.
    Set<K> keySet()
    Returns a set of the keys contained in this EnumMap.
    V put​(K key, V value)
    Maps the specified key to the specified value.
    void putAll​(Map<? extends K,​? extends V> map)
    Copies every mapping in the specified Map to this EnumMap.
    V remove​(Object key)
    Removes a mapping with the specified key from this EnumMap.
    int size()
    Returns the number of elements in this EnumMap.
    Collection<V> values()
    Returns a Collection of the values contained in this EnumMap.

    Methods inherited from class java.util.AbstractMap

    hashCode, isEmpty, toString

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.util.Map

    hashCode, isEmpty
  • Constructor Details

    • EnumMap

      public EnumMap​(Class<K> keyType)
      Constructs an empty EnumMap using the given key type.
      Parameters:
      keyType - the class object giving the type of the keys used by this EnumMap.
      Throws:
      NullPointerException - if keyType is null.
    • EnumMap

      public EnumMap​(EnumMap<K,​? extends V> map)
      Constructs an EnumMap using the same key type as the given EnumMap and initially containing the same mappings.
      Parameters:
      map - the EnumMap from which this EnumMap is initialized.
      Throws:
      NullPointerException - if map is null.
    • EnumMap

      public EnumMap​(Map<K,​? extends V> map)
      Constructs an EnumMap initialized from the given map. If the given map is an EnumMap instance, this constructor behaves in the exactly the same way as EnumMap(EnumMap)}. Otherwise, the given map should contain at least one mapping.
      Parameters:
      map - the map from which this EnumMap is initialized.
      Throws:
      IllegalArgumentException - if map is not an EnumMap instance and does not contain any mappings.
      NullPointerException - if map is null.
  • Method Details

    • clear

      public void clear()
      Removes all elements from this EnumMap, leaving it empty.
      Specified by:
      clear in interface Map<K extends Enum<K>,​V>
      Overrides:
      clear in class AbstractMap<K extends Enum<K>,​V>
      See Also:
      AbstractMap.isEmpty(), size()
    • clone

      public EnumMap<K,​V> clone()
      Returns a shallow copy of this EnumMap.
      Overrides:
      clone in class AbstractMap<K extends Enum<K>,​V>
      Returns:
      a shallow copy of this EnumMap.
    • containsKey

      public boolean containsKey​(Object key)
      Returns whether this EnumMap contains the specified key.
      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 this EnumMap contains the specified key, false otherwise.
    • containsValue

      public boolean containsValue​(Object value)
      Returns whether this EnumMap contains the specified value.
      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 this EnumMap contains the specified value, false otherwise.
    • entrySet

      public Set<Map.Entry<K,​V>> entrySet()
      Returns a Set containing all of the mappings in this EnumMap. Each mapping is an instance of Map.Entry. As the Set is backed by this EnumMap, 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.

      Specified by:
      entrySet in interface Map<K extends Enum<K>,​V>
      Specified by:
      entrySet in class AbstractMap<K extends Enum<K>,​V>
      Returns:
      a Set of the mappings.
    • equals

      public boolean equals​(Object object)
      Compares the argument to the receiver, and returns true if the specified Object is an EnumMap and both EnumMaps contain the same mappings.
      Specified by:
      equals in interface Map<K extends Enum<K>,​V>
      Overrides:
      equals in class AbstractMap<K extends Enum<K>,​V>
      Parameters:
      object - the Object to compare with this EnumMap.
      Returns:
      boolean true if object is the same as this EnumMap, false otherwise.
      See Also:
      AbstractMap.hashCode(), entrySet()
    • get

      public V get​(Object key)
      Returns the value of the mapping with the specified key.
      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.
      Returns:
      the value of the mapping with the specified key, or null if no mapping for the specified key is found.
    • keySet

      public Set<K> keySet()
      Returns a set of the keys contained in this EnumMap. The Set is backed by this EnumMap so changes to one are reflected in the other. The Set does not support adding.

      The order of the set will be the order that the enum keys were declared in.

      Specified by:
      keySet in interface Map<K extends Enum<K>,​V>
      Overrides:
      keySet in class AbstractMap<K extends Enum<K>,​V>
      Returns:
      a Set of the keys.
    • put

      public V put​(K key, V value)
      Maps the specified key to the specified value.
      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.
      value - the value.
      Returns:
      the value of any previous mapping with the specified key or null if 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 is null and this EnumMap does not support null keys or values.
    • putAll

      public void putAll​(Map<? extends K,​? extends V> map)
      Copies every mapping in the specified Map to this EnumMap.
      Specified by:
      putAll in interface Map<K extends Enum<K>,​V>
      Overrides:
      putAll in class AbstractMap<K extends Enum<K>,​V>
      Parameters:
      map - the Map to copy mappings from.
      Throws:
      UnsupportedOperationException - if adding to this EnumMap is not supported.
      ClassCastException - if the class of a key or value is inappropriate for this EnumMap.
      IllegalArgumentException - if a key or value cannot be added to this map.
      NullPointerException - if a key or value is null and this EnumMap does not support null keys or values.
    • remove

      public V remove​(Object key)
      Removes a mapping with the specified key from this EnumMap.
      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 of the mapping to remove.
      Returns:
      the value of the removed mapping or null if no mapping for the specified key was found.
      Throws:
      UnsupportedOperationException - if removing from this EnumMap is not supported.
    • size

      public int size()
      Returns the number of elements in this EnumMap.
      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 elements in this EnumMap.
    • values

      public Collection<V> values()
      Returns a Collection of the values contained in this EnumMap. The returned Collection complies with the general rule specified in Map.values(). The Collection's Iterator will return the values in the their corresponding keys' natural order (the Enum constants 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.

      Specified by:
      values in interface Map<K extends Enum<K>,​V>
      Overrides:
      values in class AbstractMap<K extends Enum<K>,​V>
      Returns:
      a collection of the values contained in this EnumMap.