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.

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>
{
    private EnumMap<K, V> enumMap;
 
    /** Creates an empty enum map with the specified key type. **/
    public EnumMapImpl(Class<K> keyType)
    {
        enumMap = new EnumMap<K, V>(keyType);
    }
 
    /**
     * Creates an enum map with the same key type as the specified enum map,
     * initially containing the same mappings (if any).
     **/
    public EnumMapImpl(EnumMap<K, ? extends V> m)
    {
        enumMap = new EnumMap<K, V>(m);
    }
 
    /** Creates an enum map initialized from the specified map. **/
    public EnumMapImpl(Map<K, ? extends V> m)
    {
        enumMap = new EnumMap<K, V>(m);
    }
 
    /** Removes all of the mappings from this map. **/
    public void clear()
    {
        enumMap.clear();
    }
 
    /** Returns true if this map contains a mapping for the specified key. **/
    public boolean containsKey(Object key)
    {
        return enumMap.containsKey(key);
    }
 
    /** Returns true if this map maps one or more keys to the specified value. **/
    public boolean containsValue(Object value)
    {
        return enumMap.containsValue(value);
    }
 
    /** Returns a Set view of the mappings contained in this map. **/
    public Set<Map.Entry<K, V>> entrySet()
    {
        return enumMap.entrySet();
    }
 
    /** Returns a Set view of the keys contained in this map. **/
    public Set<K> keySet()
    {
        return enumMap.keySet();
    }
 
    /**
     * Returns the value to which the specified key is mapped, or null if this
     * map contains no mapping for the key.
     **/
    public V get(Object key)
    {
        return enumMap.get(key);
    }
 
    /** Associates the specified value with the specified key in this map. **/
    public V put(K key, V value)
    {
        return enumMap.put(key, value);
    }
 
    /** Copies all of the mappings from the specified map to this map. **/
    public void putAll(Map<? extends K, ? extends V> map)
    {
        enumMap.putAll(map);
    }
 
    /** Removes the mapping for this key from this TreeMap if present. **/
    public V remove(Object key)
    {
        return enumMap.remove(key);
    }
 
    /** Returns the number of key-value mappings in this map. **/
    public int size()
    {
        return enumMap.size();
    }
 
    /** Returns a Collection view of the values contained in this map. **/
    public Collection<V> values()
    {
        return enumMap.values();
    }
 
    /** Returns true if this map contains no key-value mappings. **/
    public boolean isEmpty()
    {
        return enumMap.isEmpty();
    }
 
    public enum NUMBER
    {
        FIRST, SECOND, THIRD, FOURTH;
    }
 
    public static void main(String... arg)
    {
        EnumMapImpl<NUMBER, Integer> enumMap = new EnumMapImpl<NUMBER, Integer>(NUMBER.class);
        enumMap.put(NUMBER.FIRST, 100);
        enumMap.put(NUMBER.SECOND, 200);
        enumMap.put(NUMBER.THIRD, 300);
 
        System.out.println("the key set of the enumMap is ");
        Set<NUMBER> keySet = enumMap.keySet();
        Iterator<NUMBER> itr = keySet.iterator();
        while (itr.hasNext())
        {
            System.out.print(itr.next() + "\t");
        }
        System.out.println();
        System.out.println("the values of the enumMap is ");
        Collection<Integer> collectionValues = enumMap.values();
        Iterator<Integer> itr2 = collectionValues.iterator();
        while (itr2.hasNext())
        {
            System.out.print(itr2.next() + "\t");
        }
        System.out.println();
 
        System.out.println("the entry set of the enumMap is ");
        Iterator<Entry<NUMBER, Integer>> eitr;
        Set<Entry<NUMBER, Integer>> entrySet = enumMap.entrySet();
        eitr = entrySet.iterator();
        while (eitr.hasNext())
        {
            System.out.println(eitr.next() + "\t");
        }
        System.out.println("the enumMap contains Key THIRD :" + enumMap.containsKey(NUMBER.THIRD));
        System.out.println("the enumMap contains Value 600 :" + enumMap.containsValue(600));
        System.out.println("the size of the enumMap is " + enumMap.size());
        enumMap.clear();
        if (enumMap.isEmpty())
            System.out.println("the enumMap is empty");
        else
            System.out.println("the enumMap is not empty");
    }
}
$ 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

Related posts:

Spring MVC and the @ModelAttribute Annotation
Apache Commons Collections Bag
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
An Intro to Spring Cloud Security
Sắp xếp trong Java 8
Java InputStream to Byte Array and ByteBuffer
Spring REST API + OAuth2 + Angular
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java Program to Check whether Graph is Biconnected
LinkedHashSet trong java
Spring Autowiring of Generic Types
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Perform integer Partition for a Specific Case
Spring Boot - Securing Web Applications
Java Program to Implement LinkedBlockingQueue API
Create a Custom Auto-Configuration with Spring Boot
Spring 5 Testing with @EnabledIf Annotation
Guide to the Fork/Join Framework in Java
Request Method Not Supported (405) in Spring
Introduction to Spring Cloud Netflix – Eureka
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Implement Ternary Search Algorithm
Spring Security Authentication Provider
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Static Content in Spring WebFlux
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement Hash Tables with Linear Probing