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 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:
- EnumMap class is a member of the Java Collections Framework & is not synchronized.
- EnumMap is ordered collection and they are maintained in the natural order of their keys( natural order of keys means the order on which enum constant are declared inside enum type )
- It’s a high performance map implementation, much faster than HashMap.
- All keys of each EnumMap instance must be keys of a single enum type.
- EnumMap doesn’t allow null key and throw NullPointerException, at same time null values are permitted.
Methods in EnumMap
- put(K key, V value): Associates the specified value with the specified key in this map.
- putall(M map): Used to copy one map into another.
- values(): Returns the collection view of the values in map.
- remove(Object key): Used to remove a specific key from the map.
- clone(): Returns a shallow copy of the map.
- entrySet(): Returns the set view of the mappings.
- clear(): Used to remove all the mappings from the map.
- equals(Object obj): Used to compare one map with another.
- size(): Returns the number of key-value mappings in this map.
- get(Object key): Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
- containsKey(Object key): Returns true if this map contains a mapping for the specified key.
- containsValue(Object value): Returns true if this map maps one or more keys to the specified value.
- keyset(): Returns the set view of the keys/