EnumMap in java with program


In this Collection framework tutorial we will learn what is java.util.EnumMap in Collection framework in java.


Contents of page :
  • What is hierarchy of EnumMap in java?
  • java.util.EnumMap in java
  • Order of keys in EnumMap in java
  • Iterator on EnumMap in java
  • Null allowed in EnumMap in java?
  • Program to demonstrate EnumMap
  • synchronizing EnumMap in java
  • Performance of EnumMap - A comparison with HashMap in java


What is hierarchy of EnumMap in java?
-java.lang.Object
-java.util.AbstractMap
 -java.util.EnumMap

For more detailed hierarchy information read : Map hierarchy in java




java.util.EnumMap
A java.util.EnumMap is specialized Map implementation for use with enum type keys.
EnumMap all keys comes from a single enum type that is specified when the set is created.


Order of keys in EnumMap in java
The EnumMap maintains natural order (the order in which the enum constants are declared) of keys.

Iterator on EnumMap in java
The iterator returned by the iterator method in EnumMap traverses the elements in their natural order of keys(the order in which the enum constants are declared).
iterator never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the map that occur while the iteration is in progress.

Null allowed in EnumMap in java?
Null keys are not allowed in EnumMap. Attempts to insert a null key will throw NullPointerException.
But, Null values are allowed in EnumMap in java.

Example/ Program to demonstrate java.util.EnumMap in java
import java.util.Collections;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
/**
* @author AnkitMittal
*/
public class EnumMapExample{
   public enum Days{
       Monday, Tuesday;
   }
   public static void main(String args[]) {
 
     
          System.out.println("\n--------1. Create daysEnumMap -----------");
       Map<Days, String> daysEnumMap = new EnumMap<Days, String>(Days.class);
       /** All of the keys in an enumMap must come from a single
     *  enum type that is specified at declaration */
          System.out.println("\n--------2. Put key-value in daysEnumMap ");
          daysEnumMap.put(Days.Monday, "Day1");
       daysEnumMap.put(Days.Tuesday, "Day2");
          System.out.println("\n--------3. get method of daysEnumMap -----------");
       System.out.println("daysEnumMap.get(Days.Monday) : " +
                                            daysEnumMap.get(Days.Monday));
          System.out.println("\n--------4. contains method of daysEnumMap -----------");
       System.out.println("daysEnumMap.containsKey(Days.Monday) : " +
                                            daysEnumMap.containsKey(Days.Monday));
      
          System.out.println("\n--------5. Print daysEnumMap -----------");
          System.out.println(daysEnumMap);
          System.out.println("\n--------6. Print size of daysEnumMap -----------");
       System.out.println("Size of daysEnumMap : " + daysEnumMap.size());
          System.out.println("\n--------7. Iterate over keys in daysEnumMap -----------");
       Iterator<Days> keySet = daysEnumMap.keySet().iterator();
       while(keySet.hasNext()){
        Days key = keySet.next();
        System.out.println(key);
       }
          System.out.println("\n--------8. Iterate over values in daysEnumMap -----------");
       Iterator<String> values = daysEnumMap.values().iterator();
       while(values.hasNext()){
        System.out.println(values.next());
       }
          System.out.println("\n--------9. Iterate over entry in daysEnumMap -----------");
       Iterator<Entry<Days,String>> entrySet = daysEnumMap.entrySet().iterator();
       while(entrySet.hasNext()){
        System.out.println(entrySet.next());
       }
          System.out.println("\n--------10. remove method of daysEnumMap -----------");
          //remove method removes entry with specified key from daysEnumMap
          //and returns value corresponding to specified key.
       System.out.println("daysEnumMap.remove(Days.Monday) : " +
                                            daysEnumMap.remove(Days.Monday));
      
       System.out.println("\n--------11. synchronizing daysEnumMap -----------");
       Map<Days, String> map  =
                 Collections.synchronizedMap(new EnumMap<Days, String>(daysEnumMap));
   }
}
/*OUTPUT
--------1. Create daysEnumMap -----------
--------2. Put key-value in daysEnumMap
--------3. get method of daysEnumMap -----------
daysEnumMap.get(Days.Monday) : Day1
--------4. contains method of daysEnumMap -----------
daysEnumMap.containsKey(Days.Monday) : true
--------5. Print daysEnumMap -----------
{Monday=Day1, Tuesday=Day2}
--------6. Print size of daysEnumMap -----------
Size of daysEnumMap : 2
--------7. Iterate over keys in daysEnumMap -----------
Monday
Tuesday
--------8. Iterate over values in daysEnumMap -----------
Day1
Day2
--------9. Iterate over entry in daysEnumMap -----------
Monday=Day1
Tuesday=Day2
--------10. remove method of daysEnumMap -----------
daysEnumMap.remove(Days.Monday) : Day1
--------11. synchronizing daysEnumMap -----------
*/


synchronizing EnumMap in java
EnumMap is not synchronized, we can synchronize it using synchronizedSet method of Collections class.
Map<Days, String> map  = Collections.synchronizedMap(new EnumMap<Days, String>(daysEnumMap));
Days enum and daysEnumMap enumMap have been used in above java program.



Performance of java.util.EnumMap - A comparison with HashMap in java
All basic operations/methods in EnumMap execute in constant time in java.
All operations in EnumMap are generally faster than HashMap (though not always guaranteed) in java.


So in this Collection framework tutorial we learned what is java.util.EnumMap in Collection framework in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

ConcurrentHashMap in java - with Segments formation in detail with diagram


WeakHashMap in java



Basic Collection - All properties in tabular form >

Collection - List, Set and Map all properties in tabular form in java



Collection hierarchy >

Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap, WeakHashMap, EnumMap classes





Important Similarity and Differences >
Map Differences >

HashMap and Hashtable - Similarity and Differences


HashMap and ConcurrentHashMap - Similarity and Differences in java


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences in java


HashMap vs IdentityHashMap - Similarity and Differences with program in java




Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >

TreeSet vs ConcurrentSkipListSet - Similarity and Differences with program in java


TreeMap vs ConcurrentSkipListMap - Similarity and Differences with program in java





No comments:

Post a Comment