C

Core Java tutorial for beginners

Clean • Professional

EnumSet & EnumMap in Java – Examples and Usage

3 minute

EnumSet & EnumMap in Java

Both EnumSet and EnumMap are special Collection classes designed exclusively for enum types.

They are extremely fast, lightweight, memory-efficient, and perform better than traditional Set/Map implementations when working with enums.

Both belong to the java.util package.


What is EnumSet?

EnumSet is a specialized Set implementation created only for enum types. It stores enum constants using a bit-vector representation, making it extremely fast and memory-efficient.

  • Stores only enum constants
  • Backed internally by a bit vector (bitwise operations → very fast)
  • All elements must be from the same enum
  • Maintains natural order (enum declaration order)

Key Features of EnumSet

learn code with durgesh images

  • Works only with Enums --> Cannot store any other type.
  • Backed by Bit Vector --> Highly optimized for speed and memory.
  • Natural Ordering --> Order follows the enum declaration.
  • Null Not Allowed --> Adding null → NullPointerException.
  • Very Fast (O(1) operations) --> Faster than HashSet for add, remove, and contains.

Common EnumSet Methods

MethodDescription
EnumSet.of()Create a set with selected enum constants
EnumSet.allOf()Includes all enum constants
EnumSet.noneOf()Creates an empty EnumSet of given enum type
EnumSet.range(e1, e2)Includes values from e1 → e2
EnumSet.complementOf()Returns all enums not in the given set
add() / remove()Add or remove an element
contains()Check if element exists

EnumSet Examples

1. Creating an EnumSet

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }

EnumSet<Day> weekdays = EnumSet.of(Day.MON, Day.TUE, Day.WED, Day.THU, Day.FRI);

2. Selecting All Values

EnumSet<Day> allDays = EnumSet.allOf(Day.class);

3. Range Example

EnumSet<Day> midWeek = EnumSet.range(Day.TUE, Day.THU);

4. Complement Example

EnumSet<Day> weekend = EnumSet.complementOf(weekdays);

When to Use EnumSet?

Use EnumSet when:

  • You need to store multiple enum values
  • You want fast and memory-efficient operations
  • You need range or complement functionality
  • The order must follow enum declaration

Common Use Cases:

  • File permissions (READ, WRITE, EXECUTE)
  • App features / flags
  • UI access permissions
  • Days, workflow states, categories

Advantages of EnumSet

  • Extremely fast (bitwise operations)
  • Very memory-efficient
  • Natural ordering maintained
  • Rich helper methods (allOf, range, complementOf)
  • Safer and cleaner than bit masking

Limitations of EnumSet

  • Works only with enum types
  • Null not allowed
  • Not thread-safe (use Collections.synchronizedSet() if needed)

What is EnumMap?

EnumMap is a specialized Map implementation where keys must be enum constants.

It is backed internally by a simple array, making it faster and lighter than HashMap whenever the keys are enums.

  • Keys must be enum type
  • Values can be any type
  • Internally backed by an array, making it extremely fast
  • Maintains natural ordering of keys (enum declaration order)
  • Null keys not allowed, null values allowed

Key Features of EnumMap

learn code with durgesh images

  • Keys must be Enum  --> Using non-enum keys is not allowed.
  • Very Fast and Memory Efficient --> Faster than HashMap when keys are enum constants.
  • Maintains Natural Order --> Order is exactly the same as enum declaration.
  • Null Handling
  1. Null keys → throws NullPointerException
  2. Null values → allowed
  • Backed by Array --> Predictable, compact, and efficient internal structure.

EnumMap Methods

MethodDescription
put(key, value)Adds a key–value pair
get(key)Gets the value
containsKey(key)Checks if key exists
remove(key)Removes an entry
entrySet()Returns all entries
size()Returns total number of entries

EnumMap Example

Enum Declaration

enum Status { NEW, IN_PROGRESS, COMPLETED }

EnumMap Usage

EnumMap<Status, String> map = new EnumMap<>(Status.class);

map.put(Status.NEW, "Task Created");
map.put(Status.IN_PROGRESS, "Task Running");
map.put(Status.COMPLETED, "Task Done");

System.out.println(map);

Output

{NEW=Task Created, IN_PROGRESS=Task Running, COMPLETED=Task Done}

When to Use EnumMap?

Use EnumMap when:

  • Keys are enum values
  • You want fast lookup and update
  • You want predictable ordering
  • You want memory-efficient storage

Common Use Cases:

  • Finite-state machines
  • Task/workflow states
  • Configuration mapping
  • Caching enum-based data

Advantages of EnumMap

  • Faster than HashMap for enum keys
  • Very memory-efficient
  • Maintains key order
  • Type-safe and clean design

Limitations of EnumMap

  • Only enum keys allowed
  • Null keys not allowed
  • Not thread-safe (use Collections.synchronizedMap() if required)

EnumSet vs EnumMap

FeatureEnumSetEnumMap
StoresOnly enum valuesKey = Enum, Value = Any type
Internal structureBit-vectorArray-based
OrderNatural order of enumsNatural order of enum keys
PerformanceVery fastFast
Use-caseRepresenting group of enum constantsMapping enum → value


Article 0 of 0