Top Description Inners Fields Constructors Methods
java.util

public abstract sealed Class EnumSet<E extends Enum<E>>

extends AbstractSet<E extends Enum<E>>
implements Cloneable, Serializable
permits JumboEnumSet<E extends Enum<E>>, RegularEnumSet<E extends Enum<E>>
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable
Known Direct Subclasses
java.util.JumboEnumSet, java.util.RegularEnumSet
Type Parameters
<E>
the enum type of elements maintained by this set
Imports
jdk.internal.access.SharedSecrets

A specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.

The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared). The returned iterator is weakly consistent: it will never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the set that occur while the iteration is in progress.

Null elements are not permitted. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.

Like most collection implementations, EnumSet is not synchronized. If multiple threads access an enum set concurrently, and at least one of the threads modifies the set, it should be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the enum set. If no such object exists, the set should be "wrapped" using the Collections#synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access:

Set<MyEnum> s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));

Implementation Note

All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.

This class is a member of the Java Collections Framework.

Author
Josh Bloch
Since
1.5
See Also
EnumMap

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
EnumSet.SerializationProxy<E extends Enum<E>>

This class is used to serialize all EnumSet instances, regardless of implementation type.

Field Summary

Modifier and TypeField and Description
pack-priv final transient Class<E>
elementType

The class of all the elements of this set.

private static final long
pack-priv final transient Enum<?>[]
universe

All of the values comprising E.

Constructor Summary

AccessConstructor and Description
pack-priv
EnumSet(Class<E> elementType, Enum<?>[] universe)

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract void
addAll()

Adds all of the elements from the appropriate enum type to this enum set, which is empty prior to the call.

pack-priv abstract void
addRange(E from, E to)

Adds the specified range to this enum set, which is empty prior to the call.

public static <
The class of the elements in the set
E extends Enum<E>
>
EnumSet<E>

Returns:

An enum set containing all the elements in the specified type.
allOf
(Class<E>
the class object of the element type for this enum set
elementType
)

Creates an enum set containing all of the elements in the specified element type.

public EnumSet<E>

Returns:

a copy of this set
clone
()

Overrides java.lang.Object.clone.

Returns a copy of this set.

pack-priv abstract void
complement()

Complements the contents of this enum set.

public static <
The class of the elements in the enum set
E extends Enum<E>
>
EnumSet<E>

Returns:

The complement of the specified set in this set
complementOf
(EnumSet<E>
the enum set from whose complement to initialize this enum set
s
)

Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.

public static <
The class of the elements in the set
E extends Enum<E>
>
EnumSet<E>

Returns:

A copy of the specified enum set.
copyOf
(EnumSet<E>
the enum set from which to initialize this enum set
s
)

Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).

public static <
The class of the elements in the collection
E extends Enum<E>
>
EnumSet<E>

Returns:

An enum set initialized from the given collection.
copyOf
(Collection<E>
the collection from which to initialize this enum set
c
)

Hides java.util.Set.copyOf.

Creates an enum set initialized from the specified collection.

private static <E extends Enum<E>> E[]
getUniverse(Class<E> elementType)

Returns all of the values comprising E.

public static <
The class of the elements in the set
E extends Enum<E>
>
EnumSet<E>

Returns:

An empty enum set of the specified type.
noneOf
(Class<E>
the class object of the element type for this enum set
elementType
)

Creates an empty enum set with the specified element type.

public static <
The class of the specified element and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing the specified element
of
(E
the element that this set is to contain initially
e
)

Hides java.util.Set.of.

Creates an enum set initially containing the specified element.

public static <
The class of the parameter elements and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing the specified elements
of
(E
an element that this set is to contain initially
e1
,
E
another element that this set is to contain initially
e2
)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements.

public static <
The class of the parameter elements and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing the specified elements
of
(E
an element that this set is to contain initially
e1
,
E
another element that this set is to contain initially
e2
,
E
another element that this set is to contain initially
e3
)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements.

public static <
The class of the parameter elements and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing the specified elements
of
(E
an element that this set is to contain initially
e1
,
E
another element that this set is to contain initially
e2
,
E
another element that this set is to contain initially
e3
,
E
another element that this set is to contain initially
e4
)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements.

public static <
The class of the parameter elements and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing the specified elements
of
(E
an element that this set is to contain initially
e1
,
E
another element that this set is to contain initially
e2
,
E
another element that this set is to contain initially
e3
,
E
another element that this set is to contain initially
e4
,
E
another element that this set is to contain initially
e5
)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements.

public static <
The class of the parameter elements and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing the specified elements
of
(E
an element that the set is to contain initially
first
,
E...
the remaining elements the set is to contain initially
rest
)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements.

public static <
The class of the parameter elements and of the set
E extends Enum<E>
>
EnumSet<E>

Returns:

an enum set initially containing all of the elements in the range defined by the two specified endpoints
range
(E
the first element in the range
from
,
E
the last element in the range
to
)

Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.

private void
readObject(ObjectInputStream
the stream
s
)

Throws InvalidObjectException.

private void
readObjectNoData()

Throws InvalidObjectException.

pack-priv final void
typeCheck(E e)

Throws an exception if e is not of the correct type for this enum set.

pack-priv Object

Returns:

a SerializationProxy representing the state of this instance
writeReplace
()

Returns a SerializationProxy representing the state of this instance.

Inherited from java.util.AbstractSet:
equalshashCoderemoveAll

Field Detail

elementTypeback to summary
pack-priv final transient Class<E> elementType

The class of all the elements of this set.

serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@Serial
universeback to summary
pack-priv final transient Enum<?>[] universe

All of the values comprising E. (Cached for performance.)

Constructor Detail

EnumSetback to summary
pack-priv EnumSet(Class<E> elementType, Enum<?>[] universe)

Method Detail

addAllback to summary
pack-priv abstract void addAll()

Adds all of the elements from the appropriate enum type to this enum set, which is empty prior to the call.

addRangeback to summary
pack-priv abstract void addRange(E from, E to)

Adds the specified range to this enum set, which is empty prior to the call.

allOfback to summary
public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)

Creates an enum set containing all of the elements in the specified element type.

Parameters
<E>
The class of the elements in the set
elementType:Class<E>

the class object of the element type for this enum set

Returns:EnumSet<E>

An enum set containing all the elements in the specified type.

Exceptions
NullPointerException:
if elementType is null
cloneback to summary
public EnumSet<E> clone()

Overrides java.lang.Object.clone.

Returns a copy of this set.

Returns:EnumSet<E>

a copy of this set

Annotations
@SuppressWarnings:unchecked
complementback to summary
pack-priv abstract void complement()

Complements the contents of this enum set.

complementOfback to summary
public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)

Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.

Parameters
<E>
The class of the elements in the enum set
s:EnumSet<E>

the enum set from whose complement to initialize this enum set

Returns:EnumSet<E>

The complement of the specified set in this set

Exceptions
NullPointerException:
if s is null
copyOfback to summary
public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)

Creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).

Parameters
<E>
The class of the elements in the set
s:EnumSet<E>

the enum set from which to initialize this enum set

Returns:EnumSet<E>

A copy of the specified enum set.

Exceptions
NullPointerException:
if s is null
copyOfback to summary
public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)

Hides java.util.Set.copyOf.

Creates an enum set initialized from the specified collection. If the specified collection is an EnumSet instance, this static factory method behaves identically to copyOf(EnumSet). Otherwise, the specified collection must contain at least one element (in order to determine the new enum set's element type).

Parameters
<E>
The class of the elements in the collection
c:Collection<E>

the collection from which to initialize this enum set

Returns:EnumSet<E>

An enum set initialized from the given collection.

Exceptions
IllegalArgumentException:
if c is not an EnumSet instance and contains no elements
NullPointerException:
if c is null
getUniverseback to summary
private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType)

Returns all of the values comprising E. The result is uncloned, cached, and shared by all callers.

noneOfback to summary
public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)

Creates an empty enum set with the specified element type.

Parameters
<E>
The class of the elements in the set
elementType:Class<E>

the class object of the element type for this enum set

Returns:EnumSet<E>

An empty enum set of the specified type.

Exceptions
NullPointerException:
if elementType is null
ofback to summary
public static <E extends Enum<E>> EnumSet<E> of(E e)

Hides java.util.Set.of.

Creates an enum set initially containing the specified element. Overloadings of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloadings that do not use varargs.

Parameters
<E>
The class of the specified element and of the set
e:E

the element that this set is to contain initially

Returns:EnumSet<E>

an enum set initially containing the specified element

Exceptions
NullPointerException:
if e is null
ofback to summary
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements. Overloadings of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloadings that do not use varargs.

Parameters
<E>
The class of the parameter elements and of the set
e1:E

an element that this set is to contain initially

e2:E

another element that this set is to contain initially

Returns:EnumSet<E>

an enum set initially containing the specified elements

Exceptions
NullPointerException:
if any parameters are null
ofback to summary
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements. Overloadings of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloadings that do not use varargs.

Parameters
<E>
The class of the parameter elements and of the set
e1:E

an element that this set is to contain initially

e2:E

another element that this set is to contain initially

e3:E

another element that this set is to contain initially

Returns:EnumSet<E>

an enum set initially containing the specified elements

Exceptions
NullPointerException:
if any parameters are null
ofback to summary
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements. Overloadings of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloadings that do not use varargs.

Parameters
<E>
The class of the parameter elements and of the set
e1:E

an element that this set is to contain initially

e2:E

another element that this set is to contain initially

e3:E

another element that this set is to contain initially

e4:E

another element that this set is to contain initially

Returns:EnumSet<E>

an enum set initially containing the specified elements

Exceptions
NullPointerException:
if any parameters are null
ofback to summary
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements. Overloadings of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloadings that do not use varargs.

Parameters
<E>
The class of the parameter elements and of the set
e1:E

an element that this set is to contain initially

e2:E

another element that this set is to contain initially

e3:E

another element that this set is to contain initially

e4:E

another element that this set is to contain initially

e5:E

another element that this set is to contain initially

Returns:EnumSet<E>

an enum set initially containing the specified elements

Exceptions
NullPointerException:
if any parameters are null
ofback to summary
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)

Hides java.util.Set.of.

Creates an enum set initially containing the specified elements. This factory, whose parameter list uses the varargs feature, may be used to create an enum set initially containing an arbitrary number of elements, but it is likely to run slower than the overloadings that do not use varargs.

Parameters
<E>
The class of the parameter elements and of the set
first:E

an element that the set is to contain initially

rest:E[]

the remaining elements the set is to contain initially

Returns:EnumSet<E>

an enum set initially containing the specified elements

Annotations
@SafeVarargs
Exceptions
NullPointerException:
if any of the specified elements are null, or if rest is null
rangeback to summary
public static <E extends Enum<E>> EnumSet<E> range(E from, E to)

Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints. The returned set will contain the endpoints themselves, which may be identical but must not be out of order.

Parameters
<E>
The class of the parameter elements and of the set
from:E

the first element in the range

to:E

the last element in the range

Returns:EnumSet<E>

an enum set initially containing all of the elements in the range defined by the two specified endpoints

Exceptions
NullPointerException:
if from or to are null
IllegalArgumentException:
if from.compareTo(to) > 0
readObjectback to summary
private void readObject(ObjectInputStream s) throws InvalidObjectException

Throws InvalidObjectException.

Parameters
s:ObjectInputStream

the stream

Annotations
@Serial
Exceptions
InvalidObjectException:
always
readObjectNoDataback to summary
private void readObjectNoData() throws InvalidObjectException

Throws InvalidObjectException.

Annotations
@Serial
Exceptions
InvalidObjectException:
always
typeCheckback to summary
pack-priv final void typeCheck(E e)

Throws an exception if e is not of the correct type for this enum set.

writeReplaceback to summary
pack-priv Object writeReplace()

Returns a SerializationProxy representing the state of this instance.

Returns:Object

a SerializationProxy representing the state of this instance

Annotations
@Serial
java.util back to summary

private Class EnumSet.SerializationProxy<E extends Enum<E>>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

This class is used to serialize all EnumSet instances, regardless of implementation type. It captures their "logical contents" and they are reconstructed using public static factories. This is necessary to ensure that the existence of a particular implementation type is an implementation detail.

Field Summary

Modifier and TypeField and Description
private final Enum<?>[]
elements

The elements contained in this enum set.

private final Class<E>
elementType

The element type of this enum set.

private static final long
private static final Enum<?>[]

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private Object

Returns:

a EnumSet object with initial state held by this proxy
readResolve
()

Returns an EnumSet object with initial state held by this proxy.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

elementsback to summary
private final Enum<?>[] elements

The elements contained in this enum set.

elementTypeback to summary
private final Class<E> elementType

The element type of this enum set.

serialVersionUIDback to summary
private static final long serialVersionUID
Annotations
@Serial
ZERO_LENGTH_ENUM_ARRAYback to summary
private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY

Constructor Detail

SerializationProxyback to summary
pack-priv SerializationProxy(EnumSet<E> set)

Method Detail

readResolveback to summary
private Object readResolve()

Returns an EnumSet object with initial state held by this proxy.

Returns:Object

a EnumSet object with initial state held by this proxy

Annotations
@SuppressWarnings:unchecked
@Serial