The EnumMap class in Java is a specialized implementation of Map designed for enum keys, inheriting properties from both the Enum and AbstractMap classes.
EnumMap class hierarchy
The diagram below illustrates the hierarchy of the EnumMap class.
EnumMap class declaration
Let's examine the declaration of the java.util.EnumMap class.
Example
public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V> implements Serializable, Cloneable
EnumMap class Parameters
Exploring the Characteristics of the java.util.EnumMap class:
-
- : Denotes the category of keys that this map stores.
-
- : Represents the kind of values that are mapped.
Constructors of Java EnumMap class
| Constructor | Description |
|---|---|
| EnumMap(Class<K> keyType) | It is used to create an empty enum map with the specified key type. |
| EnumMap(EnumMap<K,? extends V> m) | It is used to create an enum map with the same key type as the specified enum map. |
| EnumMap(Map<K,? extends V> m) | It is used to create an enum map initialized from the specified map. |
Methods of Java EnumMap class
| SN | Method | Description |
|---|---|---|
1 |
clear() | It is used to clear all the mapping from the map. |
2 |
clone() | It is used to copy the mapped value of one map to another map. |
3 |
containsKey() | It is used to check whether a specified key is present in this map or not. |
4 |
containsValue() | It is used to check whether one or more key is associated with a given value or not. |
5 |
entrySet() | It is used to create a set of elements contained in the EnumMap. |
6 |
equals() | It is used to compare two maps for equality. |
7 |
get() | It is used to get the mapped value of the specified key. |
8 |
hashCode() | It is used to get the hashcode value of the EnumMap. |
9 |
keySet() | It is used to get the set view of the keys contained in the map. |
10 |
size() | It is used to get the size of the EnumMap. |
11 |
Values() | It is used to create a collection view of the values contained in this map. |
12 |
put() | It is used to associate the given value with the given key in this EnumMap. |
13 |
putAll() | It is used to copy all the mappings from one EnumMap to a new EnumMap. |
14 |
remove() | It is used to remove the mapping for the given key from EnumMap if the given key is present. |
Java EnumMap Example
Example
Example
import java.util.*;
public class Main {
// create an enum
public enum Days {
Monday, Tuesday, Wednesday, Thursday
};
public static void main(String[] args) {
//create and populate enum map
EnumMap<Days, String> map = new EnumMap<Days, String>(Days.class);
map.put(Days.Monday, "1");
map.put(Days.Tuesday, "2");
map.put(Days.Wednesday, "3");
map.put(Days.Thursday, "4");
// print the map
for(Map.Entry m:map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
Output:
Output
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Java EnumMap Example: Book
Example
Example
import java.util.*;
class Book {
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
}
public class Main {
// Creating enum
public enum Key{
One, Two, Three
};
public static void main(String[] args) {
EnumMap<Key, Book> map = new EnumMap<Key, Book>(Key.class);
// Creating Books
Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);
Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);
Book b3=new Book(103,"Operating System","Galvin","Wiley",6);
// Adding Books to Map
map.put(Key.One, b1);
map.put(Key.Two, b2);
map.put(Key.Three, b3);
// Traversing EnumMap
for(Map.Entry<Key, Book> entry:map.entrySet()){
Book b=entry.getValue();
System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);
}
}
}
Output:
Output
101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6