What is better to iterate over an EnumMap in java?

7.4k Views Asked by At

Suppose I have declared an enum and corresponding emummap as:

enum MyEnum {
    CONSTANT1, CONSTANT2, CONSTANT3;
}

EnumMap<MyEnum, String> MyEnumMap = new EnumMap<MyEnum, String>(MyEnum.class);

I want to iterate over MyEnumMap, for example, just to print each Entry one by one.

What is the best approach(fastest) to iterate over keys in the following cases:

  1. When it is ensured that each constant in MyEnum is a key in MyEnumMap
  2. When each constant in MyEnum may or may not be a key in MyEnumMap

I want to choose between foreach loop using MyEnumMap.keySet() or MyEnum.values(). Any other approach is most welcomed.

4

There are 4 best solutions below

0
On BEST ANSWER

If you take a look at code of EnumMap#keySet()

381  public Set<K> keySet() {
382 Set<K> ks = keySet;
383 if (ks != null)
384 return ks;
385 else
386 return keySet = new KeySet();
387 }

you will notice that it returns keySet used internally by EnumMap to store keys.

Now each time we call MyEnum.values() we are getting different array filled with all enum elements. This means that first empty array is created which later needs to be filled with all enums which requires some iteration.

So in first approach you are skipping iterating over enums already stored by map, while insecond approach we simply creating some temporary array which involves additional iteration over all MyEnum elements.

1
On

It depend on your application logic, but here are some hints:

for 1)

 // it is a bit faster to iterate over plain array, than over Set
 // And you can also get here information about entries that are in enum, but not in hashMap, so you can have logic for those cases.
 for (MyEnum e: MyEnum.values()) {
     // you can get here information what is contained and not contained in your map
}

for 2) But it is still better to use 1) because you can have there information of enum values not contained in Map.

for (MyEnum e: MyEnumMap.keySet()) {
   // you can check here all that is in your map, but you cant tell what is in enum but not in your map
}
2
On

It does not matter. Internally, EnumMap is implemented with a pair of arrays of the same length as the number of enum entries. One array has enum elements, while the second array has objects mapped to them, or NULL placeholders. Any iteration over EnumMap is therefore equivalent to a for loop on an integer index that traverses the entire range of enum ordinals, so you should pick the approach that makes your code most readable to you.

1
On

Perhaps, you just want another way of writing the code.... Since keys are always unique

for(MyEnum myEnum: MyEnum.values()){
        String value = map.get(myEnum);
         If(value != null){ 
             //use the value here
          }
 }

Just another way to write it.

Or you could also try

for (Map.Entry<MyEnum, String> entry : map.entrySet()) {       
           System.out.println(entry.getKey() + "/" + entry.getValue()); 
 }