Java Program to Implement EnumMap API

This Java program is to Implement EnumMap API.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).

Here is the source code of the Java program to Implement EnumMap API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.EnumMap;
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import java.util.Set;
  7.  
  8. public class EnumMapImpl<K extends Enum<K>, V>
  9. {
  10.     private EnumMap<K, V> enumMap;
  11.  
  12.     /** Creates an empty enum map with the specified key type. **/
  13.     public EnumMapImpl(Class<K> keyType)
  14.     {
  15.         enumMap = new EnumMap<K, V>(keyType);
  16.     }
  17.  
  18.     /**
  19.      * Creates an enum map with the same key type as the specified enum map,
  20.      * initially containing the same mappings (if any).
  21.      **/
  22.     public EnumMapImpl(EnumMap<K, ? extends V> m)
  23.     {
  24.         enumMap = new EnumMap<K, V>(m);
  25.     }
  26.  
  27.     /** Creates an enum map initialized from the specified map. **/
  28.     public EnumMapImpl(Map<K, ? extends V> m)
  29.     {
  30.         enumMap = new EnumMap<K, V>(m);
  31.     }
  32.  
  33.     /** Removes all of the mappings from this map. **/
  34.     public void clear()
  35.     {
  36.         enumMap.clear();
  37.     }
  38.  
  39.     /** Returns true if this map contains a mapping for the specified key. **/
  40.     public boolean containsKey(Object key)
  41.     {
  42.         return enumMap.containsKey(key);
  43.     }
  44.  
  45.     /** Returns true if this map maps one or more keys to the specified value. **/
  46.     public boolean containsValue(Object value)
  47.     {
  48.         return enumMap.containsValue(value);
  49.     }
  50.  
  51.     /** Returns a Set view of the mappings contained in this map. **/
  52.     public Set<Map.Entry<K, V>> entrySet()
  53.     {
  54.         return enumMap.entrySet();
  55.     }
  56.  
  57.     /** Returns a Set view of the keys contained in this map. **/
  58.     public Set<K> keySet()
  59.     {
  60.         return enumMap.keySet();
  61.     }
  62.  
  63.     /**
  64.      * Returns the value to which the specified key is mapped, or null if this
  65.      * map contains no mapping for the key.
  66.      **/
  67.     public V get(Object key)
  68.     {
  69.         return enumMap.get(key);
  70.     }
  71.  
  72.     /** Associates the specified value with the specified key in this map. **/
  73.     public V put(K key, V value)
  74.     {
  75.         return enumMap.put(key, value);
  76.     }
  77.  
  78.     /** Copies all of the mappings from the specified map to this map. **/
  79.     public void putAll(Map<? extends K, ? extends V> map)
  80.     {
  81.         enumMap.putAll(map);
  82.     }
  83.  
  84.     /** Removes the mapping for this key from this TreeMap if present. **/
  85.     public V remove(Object key)
  86.     {
  87.         return enumMap.remove(key);
  88.     }
  89.  
  90.     /** Returns the number of key-value mappings in this map. **/
  91.     public int size()
  92.     {
  93.         return enumMap.size();
  94.     }
  95.  
  96.     /** Returns a Collection view of the values contained in this map. **/
  97.     public Collection<V> values()
  98.     {
  99.         return enumMap.values();
  100.     }
  101.  
  102.     /** Returns true if this map contains no key-value mappings. **/
  103.     public boolean isEmpty()
  104.     {
  105.         return enumMap.isEmpty();
  106.     }
  107.  
  108.     public enum NUMBER
  109.     {
  110.         FIRST, SECOND, THIRD, FOURTH;
  111.     }
  112.  
  113.     public static void main(String... arg)
  114.     {
  115.         EnumMapImpl<NUMBER, Integer> enumMap = new EnumMapImpl<NUMBER, Integer>(NUMBER.class);
  116.         enumMap.put(NUMBER.FIRST, 100);
  117.         enumMap.put(NUMBER.SECOND, 200);
  118.         enumMap.put(NUMBER.THIRD, 300);
  119.  
  120.         System.out.println("the key set of the enumMap is ");
  121.         Set<NUMBER> keySet = enumMap.keySet();
  122.         Iterator<NUMBER> itr = keySet.iterator();
  123.         while (itr.hasNext())
  124.         {
  125.             System.out.print(itr.next() + "\t");
  126.         }
  127.         System.out.println();
  128.         System.out.println("the values of the enumMap is ");
  129.         Collection<Integer> collectionValues = enumMap.values();
  130.         Iterator<Integer> itr2 = collectionValues.iterator();
  131.         while (itr2.hasNext())
  132.         {
  133.             System.out.print(itr2.next() + "\t");
  134.         }
  135.         System.out.println();
  136.  
  137.         System.out.println("the entry set of the enumMap is ");
  138.         Iterator<Entry<NUMBER, Integer>> eitr;
  139.         Set<Entry<NUMBER, Integer>> entrySet = enumMap.entrySet();
  140.         eitr = entrySet.iterator();
  141.         while (eitr.hasNext())
  142.         {
  143.             System.out.println(eitr.next() + "\t");
  144.         }
  145.         System.out.println("the enumMap contains Key THIRD :" + enumMap.containsKey(NUMBER.THIRD));
  146.         System.out.println("the enumMap contains Value 600 :" + enumMap.containsValue(600));
  147.         System.out.println("the size of the enumMap is " + enumMap.size());
  148.         enumMap.clear();
  149.         if (enumMap.isEmpty())
  150.             System.out.println("the enumMap is empty");
  151.         else
  152.             System.out.println("the enumMap is not empty");
  153.     }
  154. }

$ javac  EnumMapImpl.java
$ java EnumMapImpl
the key set of the enumMap is 
FIRST	SECOND	THIRD	
the values of the enumMap is 
100	200	300	
the entry set of the enumMap is 
FIRST=100	
SECOND=200	
THIRD=300	
the enumMap contains Key THIRD :true
the enumMap contains Value 600 :false
the size of the enumMap is 3
the enumMap is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.