Introduction
The EnumMap
class in Java, part of the java.util
package, is a specialized map implementation designed for use with enum types as keys.
Table of Contents
- What is the
EnumMap
Class? - Common Methods
- Examples of Using the
EnumMap
Class - Conclusion
1. What is the EnumMap Class?
EnumMap
is a high-performance map implementation for enum keys. It is more efficient than general-purpose map implementations when using enums, providing fast access and low memory usage.
2. Common Methods
put(K key, V value)
: Associates the specified value with the specified key in the map.get(Object key)
: Returns the value to which the specified key is mapped.remove(Object key)
: Removes the mapping for the specified key from the map.containsKey(Object key)
: Returnstrue
if the map contains a mapping for the specified key.containsValue(Object value)
: Returnstrue
if the map maps one or more keys to the specified value.size()
: Returns the number of key-value mappings in the map.clear()
: Removes all mappings from the map.
3. Examples of Using the EnumMap Class
Example 1: Creating and Using an EnumMap
This example demonstrates how to create and use an EnumMap
with enum keys.
import java.util.EnumMap; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class EnumMapExample { public static void main(String[] args) { EnumMap<Day, String> schedule = new EnumMap<>(Day.class); schedule.put(Day.MONDAY, "Gym"); schedule.put(Day.TUESDAY, "Swimming"); schedule.put(Day.WEDNESDAY, "Yoga"); System.out.println("Schedule for Monday: " + schedule.get(Day.MONDAY)); } }
Output:
Schedule for Monday: Gym
Example 2: Iterating Over an EnumMap
This example shows how to iterate over the entries of an EnumMap
.
import java.util.EnumMap; import java.util.Map; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class IterateEnumMapExample { public static void main(String[] args) { EnumMap<Day, String> schedule = new EnumMap<>(Day.class); schedule.put(Day.THURSDAY, "Running"); schedule.put(Day.FRIDAY, "Rest"); for (Map.Entry<Day, String> entry : schedule.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } }
Output:
THURSDAY: Running FRIDAY: Rest
Example 3: Checking for Keys and Values
This example demonstrates how to check if an EnumMap
contains specific keys or values.
import java.util.EnumMap; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class ContainsExample { public static void main(String[] args) { EnumMap<Day, String> schedule = new EnumMap<>(Day.class); schedule.put(Day.MONDAY, "Gym"); System.out.println("Contains Monday: " + schedule.containsKey(Day.MONDAY)); System.out.println("Contains Yoga: " + schedule.containsValue("Yoga")); } }
Output:
Contains Monday: true Contains Yoga: false
4. Conclusion
EnumMap
is an efficient and type-safe map implementation for enum keys, providing fast access and minimal memory overhead. It is ideal for scenarios where the keys are known at compile time and are limited to enum types.