java.util
public abstract class EnumSet<T extends Enum<T>> extends AbstractSet<T> implements Cloneable, Serializable
Provides an efficient mechanism for recording a set of enumeration constants. As enumerations have a known set of possible values, certain assumptions can be made when creating a set of constants. The maximum size of the set will always be equal to the number of constants, and each value will always be one of these constants. As a result, the set only needs to store whether a particular constant is present or not rather than the values themselves. Each constant can thus be represented by a single bit.
This class is designed to provide an alternative to using integer bit flags by providing a typesafe Collection interface with an underlying implementation that utilises the assumptions above to give an equivalent level of efficiency. The values in a EnumSet must all be from the same Enum type, which allows the contents to be packed into a bit vector. A containment test is then simply a matter of inspecting the appropriate bit, while addition involves setting the same. Such basic operations take place in constant time.
The Iterator implementation traverses the values in the natural order
of the enumeration provided by each constant's Enum#ordinal(). It is
As is usual with most collections, the set is not synchronized by default. This can be remedied by using the synchronizedSet method. Null elements are not supported and attempts to add one will throw a NullPointerException.
Since: 1.5
Method Summary | |
---|---|
static <T extends Enum<T>> EnumSet<T> | allOf(Class<T> eltType)
Returns a set for the given enumeration type where
all the constants are present.
|
EnumSet<T> | clone()
Returns a clone of the set.
|
static <T extends Enum<T>> EnumSet<T> | complementOf(EnumSet<T> other)
Returns a set which is the inverse of the supplied set.
|
static <T extends Enum<T>> EnumSet<T> | copyOf(EnumSet<T> other)
Returns a clone of the given set.
|
static <T extends Enum<T>> EnumSet<T> | copyOf(Collection<T> other)
Creates an EnumSet using the contents of the given collection.
|
static <T extends Enum<T>> EnumSet<T> | noneOf(Class<T> eltType)
Returns a set for the given enumeration type where
none of the constants are present.
|
static <T extends Enum<T>> EnumSet<T> | of(T first)
Creates a new EnumSet populated with the given element.
|
static <T extends Enum<T>> EnumSet<T> | of(T first, T second)
Creates a new EnumSet populated with the given two elements.
|
static <T extends Enum<T>> EnumSet<T> | of(T first, T second, T third)
Creates a new EnumSet populated with the given three elements.
|
static <T extends Enum<T>> EnumSet<T> | of(T first, T second, T third, T fourth)
Creates a new EnumSet populated with the given four elements.
|
static <T extends Enum<T>> EnumSet<T> | of(T first, T second, T third, T fourth, T fifth)
Creates a new EnumSet populated with the given five elements.
|
static <T extends Enum<T>> EnumSet<T> | of(T first, T... rest)
Creates a new EnumSet populated with the given elements.
|
static <T extends Enum<T>> EnumSet<T> | range(T from, T to)
Creates a new EnumSet using the enumeration constants
starting from {@code from} and ending at {@code to} inclusive.
|
Parameters: eltType the type of enumeration to use for the set.
Returns: an EnumSet with all the bits set.
Throws: NullPointerException if the element type is null
.
Returns: a clone of the set.
Parameters: other the set to provide the complement of.
Returns: an EnumSet which is the inverse of the current one.
Throws: NullPointerException if other
is null
.
Parameters: other the set to clone.
Returns: an EnumSet that is a clone of the given set.
Throws: NullPointerException if other
is null
.
Parameters: other the collection to use to populate the new set.
Returns: an EnumSet containing elements from the given collection.
Throws: NullPointerException if other
is null
. IllegalArgumentException if the collection is empty.
Parameters: eltType the type of enumeration to use for the set.
Returns: an EnumSet with none of the bits set.
Throws: NullPointerException if the element type is null
.
Parameters: first the element to use to populate the new set.
Returns: an EnumSet containing the element.
Throws: NullPointerException if first
is null
.
Parameters: first the first element to use to populate the new set. second the second element to use.
Returns: an EnumSet containing the elements.
Throws: NullPointerException if any of the parameters are null
.
Parameters: first the first element to use to populate the new set. second the second element to use. third the third element to use.
Returns: an EnumSet containing the elements.
Throws: NullPointerException if any of the parameters are null
.
Parameters: first the first element to use to populate the new set. second the second element to use. third the third element to use. fourth the fourth element to use.
Returns: an EnumSet containing the elements.
Throws: NullPointerException if any of the parameters are null
.
Parameters: first the first element to use to populate the new set. second the second element to use. third the third element to use. fourth the fourth element to use. fifth the fifth element to use.
Returns: an EnumSet containing the elements.
Throws: NullPointerException if any of the parameters are null
.
Parameters: first the first element to use to populate the new set. rest the other elements to use.
Returns: an EnumSet containing the elements.
Throws: NullPointerException if any of the parameters are null
.
Parameters: from the element to start from. to the element to end at (may be the same as {@code from}.
Returns: an EnumSet containing the specified range of elements.
Throws: NullPointerException if any of the parameters are null
. IllegalArgumentException if {@code first.compareTo(last) > 0}.