Java EnumMap

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java EnumMap class

Java EnumMap class is the specialized Map implementation for enum keys. It inherits Enum and AbstractMap classes.

EnumMap class hierarchy

The hierarchy of EnumMap class is given in the figure given below.

EnumMap class hierarchy

EnumMap class declaration

Let's see the declaration for java.util.EnumMap class.

  1. public class EnumMap,V> extends AbstractMap implements Serializable, Cloneable  

EnumMap class Parameters

Let's see the Parameters for java.util.EnumMap class.

  • K: It is the type of keys maintained by this map.

  • V: It is the type of mapped values.

Methods of Java EnumMap class



void clear()

It is used to remove all mappings from this map.

boolean containsKey(Object key)

This method return true if this map contains a mapping for the specified key.

boolean containsValue(Object value)

This method return true if this map maps one or more keys to the specified value.

boolean equals(Object o)

It is used to compare the specified object with this map for equality.

V get(Object key)

This method returns the value to which the specified key is mapped.

V put(K key, V value)

It is used to associate the specified value with the specified key in this map.

V remove(Object key)

It is used to remove the mapping for this key.

Collection values()

It is used to return a Collection view of the values contained in this map.

int size()

It is used to return the number of key-value mappings in this map.

Constructors of Java EnumMap class



EnumMap(Class keyType)

It is used to create an empty enum map with the specified key type.

EnumMap(EnumMap m)

It is used to create an enum map with the same key type as the specified enum map.

EnumMap(Map m)

It is used to create an enum map initialized from the specified map.

Java EnumMap Example

  1. import java.util.*;  
  2. public class EnumMapExample {  
  3.    // create an enum  
  4.    public enum Days {  
  5.    Monday, Tuesday, Wednesday, Thursday  
  6.    };  
  7.    public static void main(String[] args) {  
  8.    //create and populate enum map  
  9.    EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);  
  10.    map.put(Days.Monday, "1");  
  11.    map.put(Days.Tuesday, "2");  
  12.    map.put(Days.Wednesday, "3");  
  13.    map.put(Days.Thursday, "4");  
  14.    // print the map  
  15.    for(Map.Entry m:map.entrySet()){    
  16.        System.out.println(m.getKey()+" "+m.getValue());    
  17.       }   
  18.    }  
  19. }  


Monday 1
Tuesday 2
Wednesday 3
Thursday 4

Java EnumMap Example: Book

  1. import java.util.*;    
  2. class Book {    
  3. int id;    
  4. String name,author,publisher;    
  5. int quantity;    
  6. public Book(int id, String name, String author, String publisher, int quantity) {    
  7. = id;    
  8. = name;    
  9. = author;    
  10.     this.publisher = publisher;    
  11.     this.quantity = quantity;    
  12. }    
  13. }    
  14. public class EnumMapExample {   
  15. // Creating enum  
  16.     public enum Key{  
  17.            One, Two, Three  
  18.            };  
  19. public static void main(String[] args) {    
  20.     EnumMap<Key, Book> map = new EnumMap<Key, Book>(Key.class);  
  21.     // Creating Books    
  22.     Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);    
  23.     Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);    
  24.     Book b3=new Book(103,"Operating System","Galvin","Wiley",6);    
  25.     // Adding Books to Map   
  26.        map.put(Key.One, b1);  
  27.        map.put(Key.Two, b2);  
  28.        map.put(Key.Three, b3);  
  29.     // Traversing EnumMap  
  30.        for(Map.Entry<Key, Book> entry:map.entrySet()){      
  31.             Book b=entry.getValue();    
  32.             System.out.println(" "" "" "+b.publisher+" "+b.quantity);     
  33.         }       
  34. }    
  35. }    


101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6