Questions tagged [enum-map]

`EnumMap` is optimized Map implementation exclusively for Enum keys.

Iterator over the key set takes time proportional to the number of constants in the enumerated type and returns the keys in their natural order (the order in which enums are declared).

If you are thinking of a high performance Map, EnumMap could be a decent choice for enumeration data.

All keys used in EnumMap must be from same Enum type which is specified while creating EnumMap.

Iterators are fail-fast.

You cannot insert null keys inside EnumMap.

69 questions
31
votes
2 answers

Why use EnumMap instead of HashMap

As we already have HashMap, why would we use EnumMap?
Ashish Choudhary
  • 444
  • 1
  • 4
  • 11
31
votes
3 answers

Is there a corresponding immutable enumMap in guava?

I recently learnt about the benefits of EnumMap in Java and would like to replace the existing ImmutableMap to EnumMap. However, I'd also like the immutable property offered by ImmutableMap. Is there a variant,…
brainydexter
  • 19,826
  • 28
  • 77
  • 115
29
votes
4 answers

EnumMap or HashMap if lookup key is a String

I'm trying to weigh the pros and cons of using an EnumMap over a HashMap. Since, I will always be looking up using a String, it seems that a HashMap with a String key would be the right choice. However, an EnumMap seems like better design because it…
dogbane
  • 266,786
  • 75
  • 396
  • 414
13
votes
2 answers

Why an EnumSet or an EnumMap is likely to be more performant than their hashed counterparts?

The following is from the Implementation Note section of Java doc of EnumMap : Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be faster than their HashMap counterparts. I have…
Geek
  • 26,489
  • 43
  • 149
  • 227
10
votes
2 answers

Why is EnumMap not a SortedMap in Java?

EnumMap, 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). …
rawcode
  • 105
  • 7
6
votes
3 answers

What is the best way to statically initialize an EnumMap in Java

I need to statically initialize an EnumMap.I know two ways. Using of() method of Map private static final Map<, > TEST_MAP = Map.of(ENUM_CONST1, "Value1", ENUM_CONST2, "Value2"); Using double brace initialization private…
javaguy
  • 927
  • 2
  • 16
  • 37
5
votes
2 answers

How to deserialize an EnumMap

I'm trying to figure out how to deserialize an EnumMap. I have been using the Gson library for everything else so far and have been successful. This is proving difficult. Here's a basic idea: import java.lang.reflect.Type; import…
C Taylor
  • 117
  • 1
  • 9
5
votes
1 answer

How to use generic EnumMap as parameter in abstract methods

I tried to use a generic EnumMap as paramter in an abstract method. My Problem is that when I implement the abstract method with an existing enum for the EnumMap the compiler tells me that I have to remove the Override Annotation and implement the…
4
votes
3 answers

Initialize an EnumMap from all of enum's values

I have an enum class: enum class E { A, B, C, D } What is the neatest way to initialize an EnumMap containing all of E's values as keys, each with an initial value of 0? val map = ...? assert(map is EnumMap) assert(map[E.A] ==…
k314159
  • 5,051
  • 10
  • 32
4
votes
2 answers

EnumMap constructor not found

Given that I have the requisite import java.awt.Color; import java.util.EnumMap; and enum Terrain { ... } then as far as I can tell from the documentation, this should work static EnumMap colors = new EnumMap
rwallace
  • 31,405
  • 40
  • 123
  • 242
4
votes
4 answers

Is there something like nested enums in Java?

I want to do a Country enum from which i can access to its states, how can i do that? Something like this: public enum SomeEnum { ARGENTINA { BUENOS_AIRES; } UNITED_STATES { CALIFORNIA, FLORIDA, NEW_YORK, ALASKA; …
Luis Veliz
  • 247
  • 4
  • 8
4
votes
1 answer

Initialize Map of EnumMap

I need to initialize the following private member in a JavaFX application where I am trying to organize gui widgets, however I do not know the correct syntax, could someone please let me know the correct syntax: Here is the enum I am using for my…
johnco3
  • 2,401
  • 4
  • 35
  • 67
3
votes
2 answers

Java - Why use EnumMap when enum itself can be used as a map?

I've been reading up Java recently. So I have a enum below that acts as a map with key "JESSIE" and value "My favorite cat". So why does anyone need to use EnumMap? Thanks public enum Cat { JESSIE("My favorite cat"); private String…
user2606235
  • 71
  • 1
  • 4
3
votes
1 answer

Why does EnumMap implementation check for the key's superclass?

private boolean isValidKey(Object key) { if (key == null) return false; // Cheaper than instanceof Enum followed by getDeclaringClass Class keyClass = key.getClass(); return keyClass == keyType || keyClass.getSuperclass()…
hawk
  • 1,827
  • 2
  • 14
  • 28
3
votes
1 answer

Why EnumMap internal arrays are transient

Enum maps are represented internally as arrays. K[] keyUniverse array of keys and Object[] vals array of values. These arrays are transient. Can you tell me why?
xCroNic
  • 35
  • 5
1
2 3 4 5