Mastering EnumMap in Java: Efficient and Scalable Mapping for Enums

What is EnumMap in Java?

  • In Java, an EnumMap is a specialized Map execution for utilize with Java enum keys.
  • It is part of the java.util package and gives a basically productive way to store key-value pairs where the keys are enum constants.
  • EnumMap is optimized for execution when the keys are enums, making it speedier and more memory-efficient compared to other general-purpose map usage such as HashMap or TreeMap.
  • EnumMap is a concrete class that executes the Map interface and is especially arranged for utilize with enum types as keys.
  • It uses the ordinal values of the enumeration constants to relate them with their comparing comparison values, encouraging fast lookup and addition forms.
  • This approach capitalizes on the reality that enumeration constants are ordinarily restricted in amount and arranged in a particular order.
EnumMap in Java
EnumMap in Java

Key Highlights of EnumMap in Java

1.Type Safety:
  • EnumMap guarantees type security by permitting as it were enum constants as keys.
  • This avoids type errors during runtime, as as it were the enum type indicated within the map’s statement can be utilized as a key.
2.Memory and Execution effectiveness:
  • Inside, EnumMap uses an array to store the mapping of enum constants to their values.
  • Since the number of enum constants is more as frequently as conceivable than not small and settled, this leads to more predominant execution and lower memory utilization compared to general-purpose maps, which utilize hash tables or trees.
3.Ordered:
  • EnumMap keeps up the organize of the enum constants.
  • This accumulates that the keys are ordered concurring to their clarification inside the enum type, which can be crucial when the ordering of keys is basic.
4.Null Values Not Permitted:
  • EnumMap does not allow null keys.
  • Typically since the keys are enums, and null isn’t a valid enum consistent. In any case, it does permit null as a value.

 

Types of EnumMap in Java

Java gives as it were one type of EnumMap, but it offers distinctive ways to form and initialize it. Below are a few common methods for making an EnumMap:

1.Basic EnumMap:

  • The as it were form of making an EnumMap is to initialize it with an enum type as the key type.
  • Example:

import java.util.EnumMap;

public enum Day{MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY }

public class EnumMapExample {

public static void main(String[] args) {

EnumMap<Day, String> dayActivities = new EnumMap<>(Day.class);

dayActivities.put(Day.MONDAY, “Work”);
dayActivities.put(Day.SATURDAY, “Relax”);

System.out.println(dayActivities); // Output: {MONDAY=Work, SATURDAY=Relax}
}
}

2.EnumMap with Default Values:
  • You’ll initialize the map with default values or predefined information.
  • Example:

EnumMap<Day, String> dayActivities = new EnumMap<>(Day.class);

dayActivities.put(Day.MONDAY, “Work”);

dayActivities.put(Day.SUNDAY, “Rest”);

// Output: {MONDAY=Work, SUNDAY=Rest}

3.EnumMap with Custom Data Initialization:
  • Another choice is to populate the EnumMap based on existing data, using loops or outside data sources.
  • Example:

EnumMap<Day, String> dayActivities = new EnumMap<>(Day.class);

for (Day day : Day.values()) {

dayActivities.put(day, “Default Activity”);

}

System.out.println(dayActivities);

Example Use Cases for EnumMap

  • EnumMap is particularly important in cases where the enum constants are utilized as keys and you’d like viable access to values.
  • Below is an example where EnumMap is utilized in a real-world circumstance to demonstrate a calendar system.

import java.util.EnumMap;

public class CalendarSystem {

public enum Month{ JANUARY,FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER, DECEMBER }

public static void main(String[] args) {

EnumMap<Month, Integer> monthDays = new EnumMap<>(Month.class);

monthDays.put(Month.JANUARY, 31);

monthDays.put(Month.FEBRUARY, 28);

monthDays.put(Month.MARCH, 31);

monthDays.put(Month.APRIL, 30);

monthDays.put(Month.MAY, 31);

monthDays.put(Month.JUNE, 30);

// Print the number of days in each month

for (Month month : Month.values()) {

System.out.println(month + “: ” + monthDays.get(month) + ” days”);

}

}

}

Output:

JANUARY: 31 days 

FEBRUARY: 28 days

MARCH: 31 days

APRIL: 30 days

MAY: 31 days

JUNE: 30 days

In this example, we used EnumMap to stored the number of days in each month. This gives an effective way to map enum constants (Month) to their respective values (number of days). The advantage of using EnumMap here is that it ensures fast lookups and gives a memory-efficient arrangement for handling months within the calendar.

Leave a Comment