Enum.values() vs EnumSet.allOf( ). Which one is more preferable?

Posted by Alexander Pogrebnyak on Stack Overflow See other posts from Stack Overflow or by Alexander Pogrebnyak
Published on 2010-03-17T18:42:51Z Indexed on 2010/03/17 20:51 UTC
Read the original article Hit count: 467

Filed under:
|

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 ) );

© Stack Overflow or respective owner

Related posts about java

Related posts about enum