Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Enums in Collections | Enums in Control Flow and Collections
Working with Java Enum

bookEnums in Collections

When working with Java collections, enums offer unique advantages, especially as keys or values. Java provides two specialized collectionsβ€”EnumSet and EnumMapβ€”designed specifically for use with enum types. These collections are much more efficient than their general-purpose counterparts, such as HashSet and HashMap, when you are working exclusively with enums.

Enums are efficient as keys in maps because each enum constant is a singleton and has a well-defined ordinal value. This allows collections like EnumMap to use arrays internally, which leads to faster access and less memory usage. Similarly, EnumSet is implemented as a bit vector, making operations like add, remove, and contains extremely fast. Unlike regular collections, EnumSet and EnumMap can only be used with enum types, ensuring type safety and optimal performance.

Main.java

Main.java

copy
12345678910111213141516171819202122232425
package com.example; import java.util.EnumMap; public class Main { enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public static void main(String[] args) { EnumMap<Day, String> openingHours = new EnumMap<>(Day.class); openingHours.put(Day.MONDAY, "9am - 5pm"); openingHours.put(Day.TUESDAY, "9am - 5pm"); openingHours.put(Day.WEDNESDAY, "9am - 5pm"); openingHours.put(Day.THURSDAY, "9am - 5pm"); openingHours.put(Day.FRIDAY, "9am - 5pm"); openingHours.put(Day.SATURDAY, "10am - 2pm"); openingHours.put(Day.SUNDAY, "Closed"); for (Day day : Day.values()) { System.out.println(day + ": " + openingHours.get(day)); } } }

EnumSet and EnumMap provide significant performance and memory advantages over regular collections. Because they are internally backed by arrays or bit vectors, they avoid the overhead of hashing or boxing, which is common in HashMap or HashSet. This means that operations such as lookup, insertion, and iteration are much faster and use far less memory. These collections are also type-safe and cannot contain null values, further reducing the risk of runtime errors.

Main.java

Main.java

copy
12345678910111213141516171819
package com.example; import java.util.EnumSet; public class Main { enum Feature { LOGIN, SEARCH, REPORTS, EXPORT, IMPORT } public static void main(String[] args) { EnumSet<Feature> enabledFeatures = EnumSet.of(Feature.LOGIN, Feature.SEARCH, Feature.REPORTS); System.out.println("Enabled features:"); for (Feature feature : enabledFeatures) { System.out.println(feature); } } }

1. What is the main advantage of using EnumMap over HashMap for enums?

2. Which collection is optimized for storing enum constants?

3. Can you use enums as keys in a regular HashMap?

question mark

What is the main advantage of using EnumMap over HashMap for enums?

Select the correct answer

question mark

Which collection is optimized for storing enum constants?

Select the correct answer

question mark

Can you use enums as keys in a regular HashMap?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookEnums in Collections

Swipe to show menu

When working with Java collections, enums offer unique advantages, especially as keys or values. Java provides two specialized collectionsβ€”EnumSet and EnumMapβ€”designed specifically for use with enum types. These collections are much more efficient than their general-purpose counterparts, such as HashSet and HashMap, when you are working exclusively with enums.

Enums are efficient as keys in maps because each enum constant is a singleton and has a well-defined ordinal value. This allows collections like EnumMap to use arrays internally, which leads to faster access and less memory usage. Similarly, EnumSet is implemented as a bit vector, making operations like add, remove, and contains extremely fast. Unlike regular collections, EnumSet and EnumMap can only be used with enum types, ensuring type safety and optimal performance.

Main.java

Main.java

copy
12345678910111213141516171819202122232425
package com.example; import java.util.EnumMap; public class Main { enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public static void main(String[] args) { EnumMap<Day, String> openingHours = new EnumMap<>(Day.class); openingHours.put(Day.MONDAY, "9am - 5pm"); openingHours.put(Day.TUESDAY, "9am - 5pm"); openingHours.put(Day.WEDNESDAY, "9am - 5pm"); openingHours.put(Day.THURSDAY, "9am - 5pm"); openingHours.put(Day.FRIDAY, "9am - 5pm"); openingHours.put(Day.SATURDAY, "10am - 2pm"); openingHours.put(Day.SUNDAY, "Closed"); for (Day day : Day.values()) { System.out.println(day + ": " + openingHours.get(day)); } } }

EnumSet and EnumMap provide significant performance and memory advantages over regular collections. Because they are internally backed by arrays or bit vectors, they avoid the overhead of hashing or boxing, which is common in HashMap or HashSet. This means that operations such as lookup, insertion, and iteration are much faster and use far less memory. These collections are also type-safe and cannot contain null values, further reducing the risk of runtime errors.

Main.java

Main.java

copy
12345678910111213141516171819
package com.example; import java.util.EnumSet; public class Main { enum Feature { LOGIN, SEARCH, REPORTS, EXPORT, IMPORT } public static void main(String[] args) { EnumSet<Feature> enabledFeatures = EnumSet.of(Feature.LOGIN, Feature.SEARCH, Feature.REPORTS); System.out.println("Enabled features:"); for (Feature feature : enabledFeatures) { System.out.println(feature); } } }

1. What is the main advantage of using EnumMap over HashMap for enums?

2. Which collection is optimized for storing enum constants?

3. Can you use enums as keys in a regular HashMap?

question mark

What is the main advantage of using EnumMap over HashMap for enums?

Select the correct answer

question mark

Which collection is optimized for storing enum constants?

Select the correct answer

question mark

Can you use enums as keys in a regular HashMap?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt