Enum.values()
gives you a reference to an array, and iterating over an array of enums costs the same as iterating over an array of strings. Meanwhile, comparing enum values to other enum values can actually be faster that comparing strings to strings.
Meanwhile, if you're worried about the cost of invoking the values()
method versus already having a reference to the array, don't worry. Method invocation in Java is (now) blazingly fast, and any time it actually matters to performance, the method invocation will be inlined by the compiler anyway.
So, seriously, don't worry about it. Concentrate on code readability instead, and use Enum
so that the compiler will catch it if you ever try to use a constant value that your code wasn't expecting to handle.
If you're curious about why enum comparisons might be faster than string comparisons, here are the details:
It depends on whether the strings have been interned or not. For Enum
objects, there is always only one instance of each enum value in the system, and so each call to Enum.equals()
can be done very quickly, just as if you were using the ==
operator instead of the equals()
method. In fact, with Enum
objects, it's safe to use ==
instead of equals()
, whereas that's not safe to do with strings.
For strings, if the strings have been interned, then the comparison is just as fast as with an Enum
. However, if the strings have not been interned, then the String.equals()
method actually needs to walk the list of characters in both strings until either one of the strings ends or it discovers a character that is different between the two strings.
But again, this likely doesn't matter, even in Swing rendering code that must execute quickly. :-)
@Ben Lings points out that Enum.values()
must do a defensive copy, since arrays are mutable and it's possible you could replace a value in the array that is returned by Enum.values()
. This means that you do have to consider the cost of that defensive copy. However, copying a single contiguous array is generally a fast operation, assuming that it is implemented "under the hood" using some kind of memory-copy call, rather than naively iterating over the elements in the array. So, I don't think that changes the final answer here.