X
    Categories: Java Core

Java EnumMap Example

EnumMap class implements the Map class and enables the use of enum type keys. Enum maps are maintained in the natural order of their keys. It is important to note that null keys are not permitted. If attempt has been made for adding a null key, NullPointerException will be thrown.

However, even though null keys are not allowed, null values are. Since all possible keys are known in advance, a quicker hash computation is possible.

  • EnumMap is not synchronised.
  • EnumMap is much faster than HashMap

Diagram of EnumMap

EnumMap Inheritance diagram

Constructors in EnumMap

  1. EnumMap(Class<K> keyType): creates an empty enum map with the specified key type.
  2. 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 there are any).
  3. EnumMap(Map<K, ? extends V> m): creates an enum map initialised from the specified map.



Methods in EnumMap

  1. void clear(): removes all mappings from this map.
  2. EnumMap<K, V> clone(): returns an EnumMap that represents a copy of another one.
  3. boolean containsKey(Object key): returns true if the current map contains the specified key, returns false otherwise.
  4. boolean containsValue(Object value): returns true if the current map contains the specified value, returns false otherwise.
  5. boolean equals(Object o): compares the specified object with the current map for equality.
  6. V get(Object key): returns the value to which the specified key is mapped to or null if the key contains no values.
  7. int hashCode(): returns the hash code for the current map.
  8. V put(K key, V value): Maps the specified value to the specified key.
  9. V remove(Object key): removes the specified key from the map.
  10. int size(): returns the number of pairs in the map.

For documentation for all the methods, you can visit the Oracle official documentation page.

Example of some of the methods in one program

import java.util.EnumMap; 
  
public class EnumMapExample
{ 
    public enum Hardware 
    { 
        MONITOR, MOUSE, KEYBOARD;
    } 
  
    public static void main(String args[])  
    {     
        EnumMap<Hardware, String> hardwareMapInstance = new EnumMap<Hardware, String>(Hardware.class); 

        /* hardwareMapInstance is empty. Let's populate it. */
  
        hardwareMapInstance.put(Hardware.MONITOR, "Samsung"); 
        hardwareMapInstance.put(Hardware.MOUSE, "Logitech g403"); 
        hardwareMapInstance.put(Hardware.KEYBOARD, "Razer"); 
          
        /* After having put items, let's see the map size. */
        System.out.println("Size: " + hardwareMapInstance.size()); // we could use .size() to loop through an enummap
       
        /* This would print the EnumMap in natural order. (MONITOR => MOUSE => KEYBOARD) */
        System.out.println("EnumMap: " + hardwareMapInstance); 
       
        /* Getting a specific value from an EnumMap. */
        System.out.println("Key: " + Hardware.MONITOR +" Value: " + hardwareMapInstance.get(Hardware.MONITOR)); 
       
        /* Checking if the EnumMap contains a particular key. */
        System.out.println("Does Hardware has " + Hardware.MOUSE + ": " + hardwareMapInstance.containsKey(Hardware.MOUSE)); 
       
        /* Checking if EnumMap contains a particular value */
        System.out.println("Does Hardware has " + Hardware.KEYBOARD + " : " + hardwareMapInstance.containsValue("Razer")); 

        /* Let's clear the EnumMap */
        System.out.println("Clearing the map...");
        hardwareMapInstance.clear();

        /* Let's get the size now again */
        System.out.println("The size after clear() is: " + hardwareMapInstance.size());
    } 
}

 

Output:

Size: 3
EnumMap: {MONITOR=Samsung, MOUSE=Logitech g403, KEYBOARD=Razer}
Key: MONITOR Value: Samsung
Does Hardware has MOUSE: true
Does Hardware has KEYBOARD : true
Clearing the map...
The size after clear() is: 0

 

0 0 votes
Article Rating
ym_coding:
Related Post