Java Reference
In-Depth Information
21.9.
enum
Collections
Two collections,
EnumSet
and
EnumMap
, are specifically designed for working
efficiently with
enum
constants.
21.9.1.
EnumSet
The abstract class
EnumSet<Eextends Enum<E>>
represents a set of elements
that are all enum constants from a specific enum type. Suppose you were
writing a reflection related
API
, you could define an enum of the different
field modifiers, and then have your
Field
class return the set of modifiers
applicable to that field:
enum FieldModifiers { STATIC, FINAL, VOLATILE, TRANSIENT }
public class Field {
public EnumSet<FieldModifiers> getModifiers() {
// ...
}
// ... rest of Field methods ...
}
In general, whenever an enum represents a set of flags or "status bits,"
then you will probably want to group the set of flags applicable to a given
element in an enum set.
EnumSet
objects are not created directly but are obtained from a number
of static factory methods in
EnumSet
.
public static <E extends Enum<E>> EnumSet<E>
allOf(Class<E> enumType)
Creates an
EnumSet
containing all the elements of the given
enum type.