Thursday 30 November 2017

Java Program to Implement EnumMap API


Code:

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, V>
{
    private EnumMap enumMap;

    /** Creates an empty enum map with the specified key type. **/
    public EnumMapImpl(Class keyType)
    {
        enumMap = new EnumMap(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 m)
    {
        enumMap = new EnumMap(m);
    }

    /** Creates an enum map initialized from the specified map. **/
    public EnumMapImpl(Map m)
    {
        enumMap = new EnumMap(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> entrySet()
    {
        return enumMap.entrySet();
    }

    /** Returns a Set view of the keys contained in this map. **/
    public Set 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 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 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 enumMap = new EnumMapImpl(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 keySet = enumMap.keySet();
        Iterator 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 collectionValues = enumMap.values();
        Iterator 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> eitr;
        Set> 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");
    }
}


Output:

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


More Java Programs:





















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...