EnumMap<K extends Enum<K>, V>
in Java is clearly ordered by definition of the associated enum, as you can also see in the javadoc:
Enum maps are maintained in the natural order of their keys (the order in which the enum constants are declared). This is reflected in the iterators returned by the collections views (keySet()
, entrySet()
, and values()
).
What I need is a SortedMap
using an enum as key type. I want to use methods like headMap()
or firstKey()
, but I want to profit from the added cpu+memory performance of EnumMap
s. A TreeMap
sounds like way too much overhead here.
Question: was this just missed in implementation, was it laziness (derived from AbstractMap
) or is there a good reason why EnumMap
is not a SortedMap
?
I was able to find this issue for OpenJDK. It is from 2005 yet still open/unresolved.
I'd assume there isn't any "good reason" for this being not implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With