THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
Creates an empty EnumSet that can contain elements of the given enum
type.

public static <E extends Enum<E>> EnumSet<E>copyOf(EnumSet<E>
set)

Creates an EnumSet of the same enum type as set and containing all the
elements of set.

public static <E extends Enum<E>> EnumSet<E>
complementOf(EnumSet<E> set)

Creates an EnumSet of the same enum type as set and containing all the
enum constants that are not contained in set.

public static <E extends Enum<E>> EnumSet<E>
copyOf(Collection<E> coll)

Creates an EnumSet from the given collection. If the collection is an
EnumSet, a copy is returned. Any other collection must contain one or more
elements, all of which are constants from the same enum; this enum will be
the enum type of the returned EnumSet. If coll is empty and is not an
EnumSet then IllegalArgumentException is thrown because there
is no specified enum type for this set.

The of method creates an enum set containing the given enum constant. It has five overloaded forms that take
one through five elements as arguments. For example, here is the form that takes three elements:


public static <E extends Enum<E>> EnumSet<E>of(E e1, E e2, E
e3)

Creates an EnumSet containing e1, e2, and e3.

A sixth overload takes an arbitrary number of elements:


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 other 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 ConcurrentModificationException, but it also may not reflect any changes that occur
while the iteration is in progress.

Free download pdf