Enum.values() vs EnumSet.allOf( ). Which one is more preferable?
- by Alexander Pogrebnyak
I looked under the hood for EnumSet.allOf and it looks very efficient, especially for enums with less than 64 values.
Basically all sets share the single array of all possible enum values and the only other piece of information is a bitmask which in case of allOf is set in one swoop.
On the other hand Enum.values() seems to be a bit of black magic. Moreover it returns an array, not a collection, so in many cases it must be decorated with Arrays.asList( ) to be usable in any place that expects collection.
So, should EnumSet.allOf be more preferable to Enum.values?
More specifically, which form of for iterator should be used:
for ( final MyEnum val: MyEnum.values( ) );
or
for ( final MyEnum val: EnumSet.allOf( MyEnum.values ) );