Enum Map in Java

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

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.

Enum Map Book 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 EnumMapExample {
   // Creating enum
    public enum Key{
        One, Two, Three
public static void main(String[] args) {
      EnumMap map = new EnumMap(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 entry:map.entrySet()){
      Book b=entry.getValue();
      System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);

EnumSet class

Java EnumSet class is the specialized Set implementation for use with enum types. It inherits AbstractSet class and implements the Set interface.

Enum Set Example

import java.util.*;
enum days {
public class EnumSetExample {
    public static void main(String[] args) {
    Set set = EnumSet.of(days.TUESDAY, days.WEDNESDAY);
    // Traversing elements
    Iterator iter = set.iterator();
    while (iter.hasNext())

Enum starting with number

Java does not allow the name of enum to start with number like 100A, 25K. In that case, we can append the code with _ (underscore) or any allowed pattern and make check of it.

Enum with name at beginning

public enum BookCode {
10A("Simon Haykin", "Communication System"), 
_42B("Stefan Hakins", "A Brief History of Time"), 
E1("Sedra Smith", "Electronics Circuits"); 

private String author; 
private String title; 

BookCode(String author, String title) { 
   this.author = author; 
   this.title = title; 
public String getName() { 
   String name = name(); 
   if (name.charAt(0) == '') {
       name = name.substring(1, name.length());
   return name;
public static BookCode of(String code) {
    if (Character.isDigit(code.charAt(0))) {
       code = "_" + code;
     return BookCode.valueOf(code);


Hashtable is a class in Java collections which implements Map interface and extends the Dictionary Class

Contains only unique elements and its synchronized

import java.util.*;
public class HashtableDemo {
      public static void main(String args[]) {
      // create and populate hash table
      Hashtable map = new Hashtable();
      map.put(101,"C Language");
      map.put(102, "Domain");
      map.put(104, "Databases");
      System.out.println("Values before remove: "+ map);
      // Remove value for key 102
       System.out.println("Values after remove: "+ map);

Leave a Comment