SlideShare a Scribd company logo
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

More Related Content

PDF
Storytelling For The Web: Integrate Storytelling in your Design Process
PDF
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
PDF
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
PPTX
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
PDF
Apache Camel: Jetty Component With Example
PPTX
PDF
2024 State of Marketing Report – by Hubspot
Storytelling For The Web: Integrate Storytelling in your Design Process
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
2024 Trend Updates: What Really Works In SEO & Content Marketing
Nginx A High Performance Load Balancer, Web Server & Reverse Proxy
Apache Camel: Jetty Component With Example
2024 State of Marketing Report – by Hubspot

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Lesson notes of climatology university.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
Chinmaya Tiranga quiz Grand Finale.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Lesson notes of climatology university.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Weekly quiz Compilation Jan -July 25.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
Orientation - ARALprogram of Deped to the Parents.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH
Yogi Goddess Pres Conference Studio Updates
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Ad
Ad

Enum map

  • 2. Contents ➢ What is EnumMap? ➢ Example ➢ Order of keys ➢ Concurrent Modification Exception ➢ Null Pointer Exception ➢ Synchronization ➢ Why Use EnumMap?
  • 3. 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.
  • 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 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.
  • 6. 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"); }
  • 7. 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");
  • 8. 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>(...));
  • 9. 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.