dsa

Java EnumMap

In this tutorial, we will learn about the Java EnumMap class and its operations with the help of examples.

In EnumMap, enum elements are used as keys. It implements the Map interface.
EnumMap is specialized implementation of Map interface for enumeration types. It extends AbstractMap and implements Map Interface in Java.

enummap

EnumMap class declaration

Let's see the declaration for java.util.EnumMap class.
public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable 

Few important features of EnumMap are as follows:

Methods in EnumMap

  1. put(K key, V value): Associates the specified value with the specified key in this map.
  2. putall(M map): Used to copy one map into another.
  3. values(): Returns the collection view of the values in map.
  4. remove(Object key): Used to remove a specific key from the map.
  5. clone(): Returns a shallow copy of the map.
  6. entrySet(): Returns the set view of the mappings.
  7. clear(): Used to remove all the mappings from the map.
  8. equals(Object obj): Used to compare one map with another.
  9. size(): Returns the number of key-value mappings in this map.
  10. get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
  11. containsKey(Object key): Returns true if this map contains a mapping for the specified key.
  12. containsValue(Object value): Returns true if this map maps one or more keys to the specified value.
  13. keyset(): Returns the set view of the keys/