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

后端 未结 6 1280
滥情空心
滥情空心 2020-12-23 09:05

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 singl

6条回答
  •  清歌不尽
    2020-12-23 10:00

    The values() method is more clear and performant if you just want to iterate over all possible enum values. The values are cached by the class (see Class.getEnumConstants())

    If you need a subset of values, you should use an EnumSet. Start with allOf() or noneOf() and add or remove values or use just of() as you need.

提交回复
热议问题