Java Utility Library

Java.util.EnumMap Class



Java EnumMap Class

Java.util package provides an EnumMap class which is 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 maintained in the natural order of their keys (the order in which the enum constants are declared). Null keys are not permitted. Attempts to insert a null key will throw NullPointerException. Null values are permitted.

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.

Class declaration

The declaration of java.util.EnumMap class is:

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

Class Constructors

S.NConstructors & Description
1. EnumMap(Class<K> keyType)
Creates an empty enum map with the specified key type.
2. EnumMap(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).
3. EnumMap(Map<K,? extends V> m)
Creates an enum map initialized from the specified map.

java.util.EnumMap Methods

The java.util.EnumMap class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void clear()
Removes all mappings from this map.
2. EnumMap<K,V> clone()
Returns a shallow copy of this enum map.
3. boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
4. boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
5. Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
6. boolean equals(Object obj)
Compares the specified object with this map for equality.
7. V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
8. int hashCode()
Returns the hash code value for this map.
9. Set<K> keySet()
Returns a Set view of the keys contained in this map.
10. V put(K key, V value)
Associates the specified value with the specified key in the map.
11. void putAll(Map<? extends K,? extends V> m)
Copies all of the mappings from the specified map to this map.
12. V remove(Object key)
Removes the mapping for the specified key from this map if present.
13. int size()
Returns the number of key-value pairs in the map.
14. Collection<V> values()
Returns a Collection view of the values contained in this map.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractMap<K,V>