Is there any inefficiency in calling the values() function of a specific enum class multiple times?
I have seen instances of existing code where the results of values() are cached for reuse. Is this useful?
public enum Blah {
private static final Blah [] _myValues = values()
...
public static Blah findBlahFromName(String name) {
for (Blah blah : _myValues) {
...
}
}
}
Yes, it is inefficient, but there's another way to do it that's not nearly as expensive:
EnumSet.allOf(MyEnum.class);
EnumSet has special wiring into the JDK to allow it to reuse the underlying array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With