Java Reference
In-Depth Information
public static <E extends Enum<E>> EnumSet<E>
of(E first, E... rest)
Creates an
EnumSet
containing all the specified enum values.
Finally, the
range
method takes two enum constants that define the first
and last elements that the enum set will contain. If first and last are in
the wrong order then
IllegalArgumentException
is thrown.
Once you have obtained an enum set you can freely modify it in
whatever way you need.
EnumSet
uses a bit-vector internally so it is both compact and efficient.
The iterator returned by
iterator
is not the usual fail-fast iterator of oth-
er collections. It is a
weakly consistent
iterator that returns the enum
values in their natural orderthe order in which the enum constants were
declared. A weakly consistent iterator never throws
ConcurrentModifica-
tionException
, but it also may not reflect any changes that occur while
the iteration is in progress.
21.9.2.
EnumMap
The
EnumMap<Kextends Enum<K>,V>
is a special map that uses enum values
as keys. All values in the map must come from the same enum type.
Just like
EnumSet
the enum values are ordered by their natural order, and
the iterator is weakly consistent.
Suppose you were writing an application that dynamically builds and
displays an on-line form, based on a description written in a structured
format such as
XML
. Given the set of expected form elements as an enum
enum FormElements { NAME, STREET, CITY, ZIP }
you could create an enum map to represent a filled in form, where
the keys are the form elements and the values are whatever the user
entered.