Open In App

Java Program to Implement EnumMap API

Improve
Improve
Like Article
Like
Save
Share
Report

EnumMap is a Map implementation that has enum values as its keys. Now, you all may be wondering what is enum, right? Well, enum is a user-defined data type that is generally used when all possible values are already known. 

In this article, we’ll be implementing EnumMap API in a java program. But first, we’ll be learning some of the concepts that have been used in the program and then we’ll dive right into the program.

The EnumMap API belongs to java.util package. For implementing the EnumMap API a special class is to be used, which is defined below.

EnumMapImpl<K extends Enum<K>, V> class

This class represents the use of EnumMap API for enum or enumerated data types. K stands for a key type entry and V stands for a value type entry in the map.

Example 1:

Let us consider an EnumMap with dress sizes as its keys. 

enum Dress_sizes
    {
        extra-small, small, medium, large, extra-large;
    }

Now, for say, we implement a basic feature of EnumMap API, we would like to know if a particular dress size is available or not, for this, we would use the function. enumMap.containsKey(), which would return true if the size is present else would return false.

Declaration: 

enumMap.containsKey(Dress_sizes.extra-large);
Input: extra-large
Output: true

Input: extra-extra-large
Output: false

As the size extra-large was present in the Map, it returned true and as the size extra-extra-large wasn’t present, it returned false.

Example 2:

Let’s take another such example, implementing another basic feature of the Map, for say we want to empty the above EnumMap. For this, we’ll simply use enumMap.clear() function which would clear the EnumMap.

Declaration: 

enumMap.clear();
Output: The enumMap is now empty.       
        enum Dress_sizes
        { 
                   
        }

Approach used for implementation:

  • An EnumMap class has been defined and the enum values and the key sets are pre-defined.
  • In the main function, the values have been paired with their individual key sets.
  • Basic enumMap functions are then used to perform various tasks.

Implementation:

Java




// Java program to implement enum Map API
  
import java.lang.*;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
  
public class EnumMapImpl<K extends Enum<K>, V> {
    EnumMap<K, V> enumMap;
  
    // For creating an empty enum map with the specified key
    // type.
    public EnumMapImpl(Class<K> keyType)
    {
        enumMap = new EnumMap<K, V>(keyType);
    }
  
    // For creating an enum map which will have the same key
    // type as the specified enum map,
  
    public EnumMapImpl(EnumMap<K, ? extends V> m)
    {
        enumMap = new EnumMap<K, V>(m);
    }
  
    // For Creating an enum map that is initialized from the
    // specified map.
    public EnumMapImpl(Map<K, ? extends V> m)
    {
        enumMap = new EnumMap<K, V>(m);
    }
  
    // Clears the Mappings from the enumMap.
    public void clear() { enumMap.clear(); }
    
    // Returns true if the specified value is found.
    public boolean containsValue(Object value)
    {
        return enumMap.containsValue(value);
    }
  
    // Returns true if the specified key is found.
    public boolean containsKey(Object key)
    {
        return enumMap.containsKey(key);
    }
  
    // Returns the key set of the enumMap
    public Set<K> keySet() { return enumMap.keySet(); }
  
    // Returns the entry set( values + keys) of the enumMap.
    public Set<Map.Entry<K, V> > entrySet()
    {
        return enumMap.entrySet();
    }
  
    // Returns the value to which a specified key is mapped,
    // else returns null if the specified key is not found.
  
    public V get(Object key) { return enumMap.get(key); }
  
    // Associates the specified Key and Value.
    public V put(K key, V value)
    {
        return enumMap.put(key, value);
    }
  
    // Copies the mappings of another map to the specified
    // map.
    public void putAll(Map<? extends K, ? extends V> map)
    {
        enumMap.putAll(map);
    }
  
    // Returns the size of the enumMap.
    public int size() { return enumMap.size(); }
  
    // Returns the values mapped in the specified enumMap.
    public Collection<V> values()
    {
        return enumMap.values();
    }
  
    // Returns true if the enumMap is empty.
    public boolean isEmpty() { return enumMap.isEmpty(); }
    
    // Initializing the enumMap.
    enum Week_Days {
        SUNDAY,
        MONDAY,
        TUESDAY,
        WEDNESDAY,
        THURSDAY,
        FRIDAY,
        SATURDAY;
    }
  
    // Main function.
    public static void main(String[] args)
    {
  
        EnumMapImpl<Week_Days, Integer> enumMap
            = new EnumMapImpl<Week_Days, Integer>(Week_Days.class);
        
        enumMap.put(Week_Days.SUNDAY, 1);
        enumMap.put(Week_Days.MONDAY, 2);
        enumMap.put(Week_Days.TUESDAY, 3);
        enumMap.put(Week_Days.WEDNESDAY, 4);
        enumMap.put(Week_Days.THURSDAY, 5);
        enumMap.put(Week_Days.FRIDAY, 6);
        enumMap.put(Week_Days.SATURDAY, 7);
  
        System.out.println("The size of the enumMap is: "
                           + enumMap.size());
        
        System.out.println();
  
        System.out.println("The values of the enumMap is: ");
        
        Collection<Integer> ci = enumMap.values();
        
        Iterator<Integer> iin1 = ci.iterator();
        
        while (iin1.hasNext()) 
        {
            System.out.print(iin1.next() + "\t");
        }
        
        System.out.println();
        System.out.println();
  
        System.out.println("The key set of the enumMap is: ");
        
        Set<Week_Days> ws = enumMap.keySet();
        
        Iterator<Week_Days> iin2 = ws.iterator();
        
        while (iin2.hasNext()) 
        {
            System.out.print(iin2.next() + "  ");
        }
        
        System.out.println();
        System.out.println();
        
        System.out.println("The enumMap is: ");
        
        Iterator<Entry<Week_Days, Integer> > week_iterator;
        
        Set<Entry<Week_Days, Integer> > wi = enumMap.entrySet();
        week_iterator = wi.iterator();
        
        while (week_iterator.hasNext()) 
        {
            System.out.println(week_iterator.next() + "\t");
        }
        
        System.out.println();
        System.out.println("The enumMap contains Key SUNDAY "
            + ": " + enumMap.containsKey(Week_Days.SUNDAY));
        
        System.out.println("The enumMap contains Value 1 "
                           + ": "
                           + enumMap.containsValue(1));
        
        enumMap.clear();
        
        if (enumMap.isEmpty())
            System.out.println("The EnumMap is now empty.");
        else
            System.out.println("The EnumMap is not empty.");
    }
}


Output

The size of the enumMap is: 7

The values of the enumMap is: 
1    2    3    4    5    6    7    

The key set of the enumMap is: 
SUNDAY  MONDAY  TUESDAY  WEDNESDAY  THURSDAY  FRIDAY  SATURDAY  

The enumMap is: 
SUNDAY=1    
MONDAY=2    
TUESDAY=3    
WEDNESDAY=4    
THURSDAY=5    
FRIDAY=6    
SATURDAY=7    

The enumMap contains Key SUNDAY : true
The enumMap contains Value 1 : true
The EnumMap is now empty.


Last Updated : 07 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads