-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnumMapDemo.java
More file actions
31 lines (26 loc) · 982 Bytes
/
EnumMapDemo.java
File metadata and controls
31 lines (26 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package learnCollections;
import java.util.EnumMap;
import java.util.Map;
/*
- if all the keys in Map are values of a single enum, it's recommended to use EnumMap. It has advantage of knowing all possible keys in advance
- it is more efficient compared to other implementations, as it internally uses a simple array
*/
public class EnumMapDemo {
public static void main(String[] args) {
// array of size same as enum
// [_, "Gym", _, _, _, _, _]
// no hashing
// ordinal / index is used
// faster than HashMap
// memory efficient
// ordered by enum declaration order
Map<Day, String> map = new EnumMap<>(Day.class);
map.put(Day.TUESDAY, "Gym");
map.put(Day.MONDAY, "Walk");
System.out.println(map); // {MONDAY=Walk, TUESDAY=Gym}
System.out.println(Day.TUESDAY.ordinal()); // 1 --> index
}
}
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}