Java.util.EnumMap Class



Introduction

The java.util.EnumMap class is a specialized Map implementation for use with enum keys.Following are the important points about EnumMap −

  • 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.

  • 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

Following is the declaration for java.util.EnumMap class −

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

Class constructors

Sr.No.Constructor & Description
1EnumMap(Class<K> keyType)
This constructor creates an empty enum map with the specified key type.
2EnumMap(EnumMap<K,? extends V> m)
This constructor creates an enum map with the same key type as the specified enum map, initially containing the same mappings (if any).
3EnumMap(Map<K,? extends V> m)
This constructor creates an enum map initialized from the specified map.

Class methods

Sr.No. Method & Description
1 void clear()

This method removes all mappings from this map.

2 EnumMap<K,V> clone()

This method returns a shallow copy of this enum map.

3 boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

4 boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

5 Set<Map.Entry<K,V>> entrySet()
This method returns a Set view of the mappings contained in this map.
6 boolean equals(Object o)

This method compares the specified object with this map for equality.

7 V get(Object key)

This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

8 Set<K> keySet()

This method returns a Set view of the keys contained in this map.

9 V put(K key, V value)

This method associates the specified value with the specified key in this map.

10 void putAll(Map<? extends K,? extends V> m)

This method Copies all of the mappings from the specified map to this map.

11 V remove(Object key)
This method removes the mapping for this key from this map if present.
12 int size()

This method returns the number of key-value mappings in this map.

13 Collection<V> values()

This method returns a Collection view of the values contained in this map.


Methods inherited

This class inherits methods from the following classes −

  • java.util.AbstractMap

  • java.util.Object


Advertisements