Java Collections Framework - EnumMap class

Java Collections Framework - EnumMap class


Posted in : Core Java Posted on : March 29, 2011 at 6:08 PM Comments : [ 0 ]

An Enummap is a map of all of the elements that are closely related to each other that is they come from a single enum type that is defined, explicitly or implicitly, when the map is created. It is specifically for use with enum type keys. Internal representation of this map is an array. This representation of Enum map are highly compact and effective. EnumMap class extends the AbstractMap class and implements the Map interface.� 

Java Collections Framework - EnumMap class

An Enummap is a map of all of the elements that are closely related to each other that is they come from a single enum type that is defined, explicitly or implicitly, when the map is created. It is specifically for use with enum type keys. Internal representation of this map is an array. This representation of Enum map are highly compact and effective. EnumMap class extends the AbstractMap class and implements the Map interface. 

                                In an EnumMap 'null' key are not allowed, if you will be try to insert a null key then it will display a 'NullPointerException'. There will be functioned properly if one will try to test for the presence of 'null' key or to remove. But the 'null' values are allowed. Like the other it is not synchronized. If an enum map is accessed at a time by the multiple threads and there is at least one thread alters the map, it should be synchronized externally. This is generally achieved by synchronized to some object that normally encapsulates the enum map. To keep away from an incidental unsynchronized access, there should be "wrapped" the map using Collections.synchronizedMap(java.util.Map) if there is no such object is existed.

Syntax

public class EnumMap<K extends Enum<K>,V>

Parameter description

k : Key through which the value is to be associated.

v : Value which is to be mapped with key.

Constructor of an EnumMap are :

EnumMap(Class<K> keyType) : An empty enum map is made by this constructor with the defined key.

EnumMap(EnumMap<K,? extends V> m) : This constructor makes an enum map with the key type as the defined enum map, that contains earlier same mappings (if any).

EnumMap(Map<K,? extends V> m) : This constructor makes an enum map that started from the defined map.

This class provides some methods these are as follows :

  • void clear()
  • EnumMap<K,V> clone()
  • boolean containsKey(Object key)
  • boolean containsValue(Object value)
  • Set<Map.Entry<K,V>> entrySet()
  • boolean equals(Object o)
  • V get(Object key)
  • Set<K> keySet()
  • V put(K key, V value)
  • void putAll(Map<? extends K,? extends V> m)
  • V remove(Object key)
  • int size()
  • Collection<V> values()

Example :

package devmanuals.com;
import java.util.EnumMap;
enum Day {
    Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday;
}
public class EnumMapDemo 
{
  public static void main(String args[]) 
  {
    EnumMap<Day, String> dayMap=new EnumMap<Day, String>(Day.class);
    dayMap.put(Day.Monday, "Working day");   
        dayMap.put(Day.Tuesday, "Working day");
    dayMap.put(Day.Wednesday, "Working day");
    dayMap.put(Day.Thursday, "Working day");
    dayMap.put(Day.Friday, "Working day");
    dayMap.put(Day.Saturday, "Relax day");
    dayMap.put(Day.Sunday, "Relax day");
    for(Day day:Day.values())            
    {
      System.out.println(day+"-----> is "+dayMap.get(day));    
    }
  }
}

Output :

Monday-----> is Working day

Tuesday-----> is Working day

Wednesday-----> is Working day

Thursday-----> is Working day

Friday-----> is Working day

Saturday-----> is Relax day

Sunday-----> is Relax day

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics