Menu
Topics Index
...
`


Collections Framework > Map Classes >
Siva Nookala - 15 Apr 2016
EnumMap 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 represented internally as arrays. This representation is extremely compact and efficient.

Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collections views (keySet(), entrySet(), and values()). Null keys are not permitted. Attempts to insert a null key will throw NullPointerException. Attempts to test for the presence of a null key or to remove one will, however, function properly. Null values are permitted.

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


Constructors :
EnumMap(Class<K> keyType)
Creates an empty enum map with the specified key type.

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

EnumMap(Map<K,? extends V> m)
Creates an enum map initialized from the specified map.

EnumMap Methods :
Method Description
void clear() This method removes all mappings from this map.
EnumMap<K,V> clone() This method returns a shallow copy of this enum map.
boolean containsKey(Object key) This method returns true if this map contains a mapping for the specified key.
boolean containsValue(Object value) This method returns true if this map maps one or more keys to the specified value.
Set < Map.Entry <K, V> > entrySet() This method returns a Set view of the mappings contained in this map.
boolean equals(Object o) This method compares the specified object with this map for equality.
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.
Set<K> keySet() This method returns a Set view of the keys contained in this map.
put(K key, V value) This method associates the specified value with the specified key in this map.
void putAll(Map<? extends K,? extends V> m) This method Copies all of the mappings from the specified map to this map.
remove(Object key) This method removes the mapping for this key from this map if present.
int size() This method returns the number of key-value mappings in this map.
Collection<V> values() This method returns a Collection view of the values contained in this map.
EnumMapDemo
import java.util.*;

class EnumMapDemo
{
    public static void main(String arg[])
    {
        EnumMap map = new EnumMap<>(Numbers.class); // LINE A
        map.put(Numbers.One, 1);
        map.put(Numbers.Two, 2);
        map.put(Numbers.Three, 3);
        map.put(Numbers.Four, 4);
        map.put(Numbers.Five, 5);
        System.out.println(map); // LINE B
        Set set = map.keySet(); // Returns set view of keys // LINE C
        System.out.println(set);
        System.out.println(map.get(Numbers.Four)); // LINE D
        // to remove one object
        map.remove(Numbers.Four); // LINE E
        System.out.println(map);
        //calling set iterator
        Iterator it = set.iterator(); // LINE F
        //getting keys and values using iterator
        while (it.hasNext())
        {
            Numbers n = (Numbers) it.next();
            System.out.println("Key = " + n + ", Value = " + map.get(n));
        }
    
    }
}

enum Numbers
{
    One, Two, Three, Four, Five;
}
OUTPUT

{One=1, Two=2, Three=3, Four=4, Five=5}
[One, Two, Three, Four, Five]
4
{One=1, Two=2, Three=3, Five=5}
Key = One, Value = 1
Key = Two, Value = 2
Key = Three, Value = 3
Key = Five, Value = 5

DESCRIPTION

In the above program we have demonstrated the working of EnumMap at LINE A we created a EnumMap and added elements of Numbers an enum class and displayed it at LINE B. At LINE C we are getting the set view of the keys in the EnumMap by keySet method. At LINE D we are getting the value of Four using get method. At LINE E we remove Four from the map using remove method and the modified map is displayed. At LINE F we are calling the set iterator method and using it we have displayed the keys and values in the EnumMap.

THINGS TO TRY
  • Try to add one more object SpecialDay with value 10 to the map. Use the below sample code.
    map.put("SpecialDay", 10);
    The above code will throw a runtime error since all the elements in an EnumMap must from a single enum class.
  • Check whether Four is still in the map.
    System.out.println(map.containsKey(Numbers.Four));
    The output will be false since Four is removed from the map.
  • Remember EnumMap don't implement iterators hasPervious() and previous() methods

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.