Enum Map , EnumSet class,Enum starting with number and Hashtable


Enum Map

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.

Example

import java.util.*;
 
class LibraryBook { 
    int bookID; 
    String title, writer, publisher; 
    int stock; 
 
    public LibraryBook(int bookID, String title, String writer, String publisher, int stock) { 
        this.bookID = bookID; 
        this.title = title; 
        this.writer = writer; 
        this.publisher = publisher; 
        this.stock = stock; 
    } 
}
 
public class BookInventory { 
    public enum BookCategory { 
        Fiction, NonFiction, Reference
    }; 
 
    public static void main(String[] args) { 
        EnumMap<BookCategory, LibraryBook> libraryMap = new EnumMap<>(BookCategory.class); 
 
        LibraryBook book1 = new LibraryBook(101, "To Kill a Mockingbird", "Harper Lee", "Harper Collins", 5); 
        LibraryBook book2 = new LibraryBook(102, "Sapiens: A Brief History of Humankind", "Yuval Noah Harari", "Vintage", 10); 
        LibraryBook book3 = new LibraryBook(103, "The Alchemist", "Paulo Coelho", "HarperOne", 7); 
 
        libraryMap.put(BookCategory.Fiction, book1); 
        libraryMap.put(BookCategory.NonFiction, book2); 
        libraryMap.put(BookCategory.Reference, book3); 
 
        for (Map.Entry<BookCategory, LibraryBook> entry : libraryMap.entrySet()) { 
            LibraryBook book = entry.getValue(); 
            System.out.println(book.bookID + " " + book.title + " " + book.writer + " " + book.publisher + " " + book.stock); 
        } 
    } 
}

EnumSet class

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

Example

import java.util.*;
 
enum Months {
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY
}
 
public class EnumSetMonthsExample {
    public static void main(String[] args) {
        Set<Months> monthSet = EnumSet.of(Months.APRIL, Months.MAY, Months.JUNE);
 
        // Traversing elements
        Iterator<Months> iterator = monthSet.iterator();
        while (iterator.hasNext())
            System.out.println(iterator.next());
    }
}

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.

Example

public enum LibraryBooks {
    BOOK_1("William Shakespeare", "Hamlet"),
    BOOK_2("Jane Austen", "Pride and Prejudice"),
    BOOK_3("George Orwell", "1984");
 
    private final String author;
    private final String title;
 
    LibraryBooks(String author, String title) {
        this.author = author;
        this.title = title;
    }
 
    public String getBookTitle() {
        return title;
    }
 
    public static LibraryBooks findByTitle(String title) {
        for (LibraryBooks book : LibraryBooks.values()) {
            if (book.getBookTitle().equalsIgnoreCase(title)) {
                return book;
            }
        }
        throw new IllegalArgumentException("Book with title '" + title + "' not found");
    }
}

Hashtable

Hashtable is a class in Java collections which implements Map interface and extends the Dictionary Class. Contains only unique elements and its synchronized

Example

import java.util.*;
 
public class ClassroomSubjects {
    public static void main(String args[]) {
        // Create and populate a hash table
        Hashtable<String, String> subjects = new Hashtable<String, String>();
        subjects.put("Math", "Algebra");
        subjects.put("Science", "Biology");
        subjects.put("History", "World Wars");
        subjects.put("Literature", "Shakespeare");
 
        System.out.println("Subjects before removal: " + subjects);
 
        // Remove value for the key "Science"
        subjects.remove("Science");
 
        System.out.println("Subjects after removal: " + subjects);
    }
}

Basic Programs