img
The EnumSet Class
EnumSet extends AbstractSet and implements Set. It is specifically for use with keys of an
enum type. It is a generic class that has this declaration:
class EnumSet<E extends Enum<E>>
Here, E specifies the elements. Notice that E must extend Enum<E>, which enforces the
requirement that the elements must be of the specified enum type.
EnumSet defines no constructors. Instead, it uses the factory methods shown in Table 17-7
to create objects. All methods can throw NullPointerException. The copyOf( ) and range( )
methods can also throw IllegalArgumentException. Notice that the of( ) method is overloaded
a number of times. This is in the interest of efficiency. Passing a known number of arguments
can be faster than using a vararg parameter when the number of arguments is small.
Accessing a Collection via an Iterator
Often, you will want to cycle through the elements in a collection. For example, you might
want to display each element. One way to do this is to employ an iterator, which is an object
that implements either the Iterator or the ListIterator interface. Iterator enables you to cycle
through a collection, obtaining or removing elements. ListIterator extends Iterator to allow
Method
Description
static <E extends Enum<E>>
Creates an EnumSet that contains the elements in the
EnumSet<E> allOf(Class<E> t)
enumeration specified by t.
static <E extends Enum<E>> EnumSet<E>
Creates an EnumSet that is comprised of those elements not
complementOf(EnumSet<E> e)
stored in e.
Creates an EnumSet from the elements stored in c.
static <E extends Enum<E>>
EnumSet<E> copyOf(EnumSet<E> c)
Creates an EnumSet from the elements stored in c.
static <E extends Enum<E>>
EnumSet<E> copyOf(Collection<E> c)
static <E extends Enum<E>>
Creates an EnumSet that contains the elements that are not in
EnumSet<E> noneOf(Class<E> t)
the enumeration specified by t, which is an empty set by definition.
Creates an EnumSet that contains v and zero or more
static <E extends Enum<E>>
EnumSet<E> of(E v, E ... varargs)
additional enumeration values.
Creates an EnumSet that contains v.
static <E extends Enum<E>>
EnumSet<E> of(E v)
Creates an EnumSet that contains v1 and v2.
static <E extends Enum<E>>
EnumSet<E> of(E v1, E v2)
Creates an EnumSet that contains v1 through v3.
static <E extends Enum<E>>
EnumSet<E> of(E v1, E v2, E v3)
Creates an EnumSet that contains v1 through v4.
static <E extends Enum<E>>
EnumSet<E> of(E v1, E v2, E v3, E v4)
Creates an EnumSet that contains v1 through v5.
static <E extends Enum<E>>
EnumSet<E> of(E v1, E v2, E v3, E v4,
E v5)
static <E extends Enum<E>>
Creates an EnumSet that contains the elements in the range
EnumSet<E> range(E star t, E end)
specified by star t and end.
TABLE 17-7
The Methods Defined by EnumSet
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home