Enum Map
https://techognite.com
Complete coverage on tech
Contents
➢ What is EnumMap?
➢ Example
➢ Order of keys
➢ Concurrent Modification Exception
➢ Null Pointer Exception
➢ Synchronization
➢ Why Use EnumMap?
What is EnumMap?
A specialized Map implementation for use with enum type
keys.
All of the keys in an enum map must come from a single
enum type that is specified when the map is created.
Enum maps are represented internally as arrays. This
representation is extremely compact and efficient.
This class is a member of the Java Collections Framework
Enum Maps is the part of java Since jdk1.5.
Example
public enum WeekDaysEnum {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY;
}
EnumMap<WeekDaysEnum,String> enumMap = new
EnumMap<>(WeekDaysEnum.class);
enumMap.put(WeekDaysEnum.SUNDAY, "SUNDAY");
enumMap.put(WeekDaysEnum.MONDAY, "MONDAY");
for(Entry<WeekDaysEnum, String> week:enumMap.entrySet()){
System.out.println(week.getKey() + " : " + week.getValue());
}
Order of keys
Enum maps are maintained in the natural
order of their keys (the order in which the enum
constants are declared) .
Means iteration will always fetch results
according to enum constant order.
Concurrent Modification Exception
Iterators returned by the EnumMap will never
throw ConcurrentModificationException.
Iterator may or may not show the effects of any
modifications to the map that occur while the iteration is
in progress.
for (Entry<WeekDaysEnum, String> week : enumMap.entrySet()) {
System.out.println(week.getKey().name() + " : " + week.getValue());
enumMap.put(WeekDaysEnum.WEDNESDAY, "WEDNESDAY");
}
Null Pointer Exception
Null keys are not permitted. Attempts to insert
a null key will throw NullPointerException.
EnumMap<WeekDaysEnum, String> enumMap = new
EnumMap<>(WeekDaysEnum.class);
enumMap.put(null, "FRIDAY");
Synchronization
EnumMap is not synchronized.
If multiple threads access an enum map
concurrently, and modifying it then it should be
synchronized.
Map<EnumKey, V> m = Collections.synchronizedMap(new
EnumMap<EnumKey, V>(...));
Why Use EnumMap
All basic operations execute in constant time.
They are likely (though not guaranteed) to be
faster than their HashMap.
EnumMap Internally uses array in place of
hashing technique.
Thanks

Enum map

  • 1.
  • 2.
    Contents ➢ What isEnumMap? ➢ Example ➢ Order of keys ➢ Concurrent Modification Exception ➢ Null Pointer Exception ➢ Synchronization ➢ Why Use EnumMap?
  • 3.
    What is EnumMap? Aspecialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient. This class is a member of the Java Collections Framework Enum Maps is the part of java Since jdk1.5.
  • 4.
    Example public enum WeekDaysEnum{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY; } EnumMap<WeekDaysEnum,String> enumMap = new EnumMap<>(WeekDaysEnum.class); enumMap.put(WeekDaysEnum.SUNDAY, "SUNDAY"); enumMap.put(WeekDaysEnum.MONDAY, "MONDAY"); for(Entry<WeekDaysEnum, String> week:enumMap.entrySet()){ System.out.println(week.getKey() + " : " + week.getValue()); }
  • 5.
    Order of keys Enummaps are maintained in the natural order of their keys (the order in which the enum constants are declared) . Means iteration will always fetch results according to enum constant order.
  • 6.
    Concurrent Modification Exception Iteratorsreturned by the EnumMap will never throw ConcurrentModificationException. Iterator may or may not show the effects of any modifications to the map that occur while the iteration is in progress. for (Entry<WeekDaysEnum, String> week : enumMap.entrySet()) { System.out.println(week.getKey().name() + " : " + week.getValue()); enumMap.put(WeekDaysEnum.WEDNESDAY, "WEDNESDAY"); }
  • 7.
    Null Pointer Exception Nullkeys are not permitted. Attempts to insert a null key will throw NullPointerException. EnumMap<WeekDaysEnum, String> enumMap = new EnumMap<>(WeekDaysEnum.class); enumMap.put(null, "FRIDAY");
  • 8.
    Synchronization EnumMap is notsynchronized. If multiple threads access an enum map concurrently, and modifying it then it should be synchronized. Map<EnumKey, V> m = Collections.synchronizedMap(new EnumMap<EnumKey, V>(...));
  • 9.
    Why Use EnumMap Allbasic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap. EnumMap Internally uses array in place of hashing technique.
  • 10.