java - Optimization done by an EnumMap/EnumSet -
i read article enummap
. it's written "using enummap brings implementation specific benefits done enum keys, in short enummap
optimized map implementation exclusively enum keys."
it's written "enum implemented using arrays , common operations result in constant time. if thinking of high performance map
, enummap
decent choice enumeration data."
can point me out how optimizations done ? ("operations result in constant time")
looking @ documentation enummap:
a specialized
map
implementation use enum type keys. of keys in enum map must come single enum type specified, explicitly or implicitly, when map created. enum maps represented internally arrays. representation extremely compact , efficient.enum maps maintained in natural order of keys (the order in enum constants declared). reflected in iterators returned collections views (
keyset()
,entryset()
, ,values()
).
in short, enummap
array, of type of values of map. in other words, enummap<someenum, somevalue>
, somevalue[]
.
how indexes assigned, might ask? assigned natural order of enum. example:
enum day { mon, tue, wed, thu, fri, sat, sun }
the above enum has following natural order.
mon tue wed thu fri sat sun 0 1 2 3 4 5 6
so, operation map.put(day.fri, "yay!")
can viewed as:
array[4] = "yay!";
array access constant time operation, , that's why enummap
benefits of well. (get()
) works same way.
Comments
Post a Comment