Skip to content

Commit 892e920

Browse files
committed
Internal working — java.util.EnumMap (detailed)
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent a09d96e commit 892e920

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Internal working — java.util.EnumMap (detailed)
2+
3+
Overview
4+
--------
5+
- EnumMap is a specialized implementation of the Map interface designed to work **only with enum keys**.
6+
- Internally, it uses an **array** rather than hashing.
7+
- Keys are stored and retrieved using their **ordinal value** (the position of the enum constant in its declaration).
8+
- EnumMap is **faster and more memory-efficient** than HashMap when the keys are enums.
9+
10+
Core ideas
11+
----------
12+
1. **Backing structure**: An Object[] array of size equal to the number of enum constants.
13+
- For example, if `Day` has 7 constants → internal array length = 7.
14+
15+
2. **Index mapping**:
16+
- Key → ordinal() → index in array.
17+
- No need to compute `hashCode()`. Example: `Day.MONDAY.ordinal() = 0`, `Day.TUESDAY.ordinal() = 1`.
18+
19+
3. **Ordering**:
20+
- EnumMap preserves the **natural order of enum constants** (as declared in the enum).
21+
- Iteration always happens in enum declaration order, not insertion order.
22+
23+
4. **Null handling**:
24+
- Null keys are NOT allowed (`NullPointerException` if attempted).
25+
- Null values ARE allowed.
26+
27+
5. **Performance**:
28+
- All operations (put, get, remove, containsKey) are **O(1)** due to direct array index lookup.
29+
30+
6. **Memory efficiency**:
31+
- Very compact; avoids hashing overhead and entry node wrappers.
32+
- Perfect when you know your keys are restricted to a single enum type.
33+
34+
ASCII visualization
35+
-------------------
36+
Suppose we have enum:
37+
38+
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
39+
40+
Internally EnumMap creates an array of length 7 (number of constants):
41+
42+
Index: 0 1 2 3 4 5 6
43+
Enum: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY
44+
Value: "Walk" "Gym" null null null null "Sleep Relax"
45+
46+
So:
47+
- map.put(Day.MONDAY, "Walk") → array[0] = "Walk"
48+
- map.put(Day.TUESDAY, "Gym") → array[1] = "Gym"
49+
- map.put(Day.SUNDAY, "Sleep Relax") → array[6] = "Sleep Relax"
50+
51+
Iteration always visits indices 0 → 6 in enum order.
52+
53+
How operations work
54+
-------------------
55+
56+
PUT
57+
---
58+
1. Key = enum constant (e.g., Day.TUESDAY).
59+
2. Find index = key.ordinal() → 1.
60+
3. Store value at array[1].
61+
4. If already present, overwrite value.
62+
63+
GET
64+
---
65+
1. Key = Day.TUESDAY.
66+
2. Index = key.ordinal() → 1.
67+
3. Return array[1] → "Gym".
68+
69+
REMOVE
70+
------
71+
1. Key = Day.MONDAY.
72+
2. Index = 0.
73+
3. Set array[0] = null.
74+
75+
Iteration
76+
---------
77+
- Always from ordinal 0 → maxOrdinal.
78+
- Skips nulls (i.e., if a key has no value assigned).
79+
- Preserves natural enum order.
80+
81+
Rehashing
82+
---------
83+
- No rehashing required (unlike HashMap/Hashtable).
84+
- Array length is fixed = number of enum constants.
85+
- Only values are replaced, not keys (keys are implicit from enum).
86+
87+
Complexity
88+
----------
89+
- put/get/remove: O(1)
90+
- containsKey: O(1)
91+
- iteration: O(n) where n = number of enum constants.
92+
93+
When to use EnumMap
94+
-------------------
95+
✔ When keys are enums (Day, State, Direction, etc.).
96+
✔ Need predictable iteration order (enum declaration order).
97+
✔ When performance and memory efficiency matter.
98+
✘ Do not use if keys are not enums (compile-time restriction).
99+
✘ Avoid if you need null keys.
100+
101+
Practical applications
102+
----------------------
103+
- Scheduling tasks by Day enum (e.g., MONDAY=Meeting, FRIDAY=Report).
104+
- State machines (enum states mapped to actions).
105+
- Configuration keyed by enum (e.g., LogLevel.DEBUG → config value).
106+
- Small fixed-domain mappings where enum keys are natural choice.
107+
108+
Example flow
109+
------------
110+
map.put(Day.MONDAY, "Walk")
111+
map.put(Day.TUESDAY, "Gym")
112+
map.put(Day.SUNDAY, "Sleep Relax")
113+
114+
Iteration → {MONDAY=Walk, TUESDAY=Gym, SUNDAY=Sleep Relax}
115+
(order = enum declaration order, not insertion)

0 commit comments

Comments
 (0)