EnumMap in Java

EnumMap

In the previous blog, we learned about WeakHashMap. If you want to learn more about it, visit WeakHashMap in Java. In this blog, we will go through another collection class i.e. EnumMap.


EnumMap

EnumMap class is a specialized implementation of Map for enum type. It extends AbstractClass and implements the Map interface. The EnumMap class keeps the keys in their natural sequence. It is not synchronized. EnumMap class cannot have null keys, if there is a null key found then it gives NullPointerException. In order to use EnumMap class we must import the package called java.util.EnumMap.


Creating EnumMap

In order to create an EnumMap we need to import the package called java.util.EnumMap.

e.g.

enum Days {
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
EnumMap<Days, String> enumMap = EnumMap<>(Days.class);

Methods of EnumMap

  1. put()
  2. put() method adds or inserts the specified key/value mapping to the enum map.

  3. putAll()
  4. putAll() method adds or inserts all the elements from the specified map to this map.

    Example of EnumMap using the above two methods

    import java.util.EnumMap;
    class EnumMapPutEx {
    
      enum Days {
          MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
      }
      public static void main(String[] args) {
        EnumMap<Days, String> days = new EnumMap<>(Days.class);
        days.put(Days.MONDAY, "1");
        days.put(Days.TUESDAY, "2");
        days.put(Days.WEDNESDAY, "3");
        System.out.println("EnumMap: " + days);
        EnumMap<Days, String> days1 = new EnumMap<>(Days.class);
        days1.put(Days.FRIDAY, "3");
        days1.putAll(days);
        System.out.println("EnumMap2: " + days1);
      }
    }

    Output

    EnumMap: {MONDAY=1, TUESDAY=2, WEDNESDAY=3}
    EnumMap2: {MONDAY=1, TUESDAY=2, WEDNESDAY=3, FRIDAY=3}
  5. get()
  6. get() method returns the value of the specified key in the enum map.

  7. entrySet()
  8. entrySet() method returns a set of all the key-value mapping of the enum map.

  9. keySet()
  10. keySet() method returns a set view of the keys contained in the enum map.

  11. values()
  12. values() method returns the view of all the values present in the entries of the enum map.

    Example of EnumMap using the above methods

    import java.util.EnumMap;
    class EnumMapMethodsEx {
      enum Days {
          MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
      }
      public static void main(String[] args) {
        EnumMap<Days, String> days = new EnumMap<>(Days.class);
        days.put(Days.MONDAY, "1");
        days.put(Days.TUESDAY, "2");
        days.put(Days.WEDNESDAY, "3");
        days.put(Days.THURSDAY, "4");
        days.put(Days.FRIDAY, "5");
        days.put(Days.SATURDAY, "6");
        days.put(Days.SUNDAY, "7");
        System.out.println("EnumMap: " + days);
        System.out.println("Displaying the output of EnumMap after using get() method:" + days.get(Days.SATURDAY));
        System.out.println("Displaying the output of EnumMap after using entrySet() method:" + days.entrySet());
        System.out.println("Displaying the output of EnumMap after using keySet() method:" + days.keySet());
        System.out.println("Displaying the output of EnumMap after using values() method:" + days.values());
      }
    }

    Output

    EnumMap: {MONDAY=1, TUESDAY=2, WEDNESDAY=3, THURSDAY=4, FRIDAY=5, SATURDAY=6, SUNDAY=7}
    Displaying the output of EnumMap after using get() method:6
    Displaying the output of EnumMap after using entrySet() method:[MONDAY=1, TUESDAY=2, WEDNESDAY=3, THURSDAY=4, FRIDAY=5, SATURDAY=6, SUNDAY=7]
    Displaying the output of EnumMap after using keySet() method:[MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY]
    Displaying the output of EnumMap after using values() method:[1, 2, 3, 4, 5, 6, 7]
  13. get()
  14. get() method returns the value of the specified key in the map.

  15. get()
  16. get() method returns the value of the specified key in the map.

  17. getOrDefult()
  18. The getOrDefult() method returns the value associated with the given key. If the key cannot be discovered, it returns the specified default value.

  19. remove()
  20. The remove() function removes or deletes the supplied values from the enum map together with the related specified.

  21. clear()
  22. clear() method removes all the key-value pairs of the map.

  23. size()
  24. size() method returns the number of entries in the enum map.

  25. isEmpty()
  26. isEmpty() method checks whether the weakhashmap is empty or not.

  27. containsKey()
  28. containsKey() method checks if the mapping for the specified key is present in the enum map. If the key is missing, it returns false.

  29. containsValue()
  30. containsValue() method checks if the specified value is present in one or more mappings of the enum map or not. If the value is not present then it returns false.

  31. clone()
  32. clone() method returns the copy of the EnumMap.

    Example of EnumMap using the above methods

    import java.util.EnumMap;
      class EnumMapMethodsEx {
    
        enum Days {
            MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
        }
        public static void main(String[] args) {
          EnumMap<Days, String> days = new EnumMap<>(Days.class);
          days.put(Days.MONDAY, "1");
          days.put(Days.TUESDAY, "2");
          days.put(Days.WEDNESDAY, "3");
          days.put(Days.THURSDAY, "4");
          days.put(Days.FRIDAY, "5");
          days.put(Days.SATURDAY, "6");
          days.put(Days.SUNDAY, "7");
          System.out.println("EnumMap: " + days);
          System.out.println("Displaying the output of EnumMap after using size() method:" + days.size());
          System.out.println("Element to be removed :" + days.remove(Days.MONDAY));
          System.out.println("Displaying the output of EnumMap after using remove() method:" + days);
          System.out.println("Does the EnumMap contains the specific key:" + days.containsKey(Days.SUNDAY));
          System.out.println("Does the EnumMap contains the specific value:" + days.containsValue("7"));
          EnumMap<Days, String> days1 = new EnumMap<>(Days.class);
          days1 = days.clone();
          System.out.println("Cloned EnumMap: " + days1);
          days.clear();
          System.out.println("Displaying the output of EnumMap after using clear() method:" + days);
        }
      }

    Output

    EnumMap: {MONDAY=1, TUESDAY=2, WEDNESDAY=3, THURSDAY=4, FRIDAY=5, SATURDAY=6, SUNDAY=7}
    Displaying the output of EnumMap after using size() method:7
    Element to be removed :1
    Displaying the output of EnumMap after using remove() method:{TUESDAY=2, WEDNESDAY=3, THURSDAY=4, FRIDAY=5, SATURDAY=6, SUNDAY=7}
    Does the EnumMap contains the specific key:true
    Does the EnumMap contains the specific value:true
    Cloned EnumMap: {TUESDAY=2, WEDNESDAY=3, THURSDAY=4, FRIDAY=5, SATURDAY=6, SUNDAY=7}
    Displaying the output of EnumMap after using clear() method:{}
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.