EnumSet

abstract class EnumSet<E : Enum<E>>(elementType: KClass<E>, universe: Array<Enum<E>>) : AbstractMutableSet<E> , Cloneable<EnumSet<E>>

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 {@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework.

Author

Josh Bloch

Since

1.5

Parameters

the enum type of elements maintained by this set

See also

EnumMap

Inheritors

Constructors

Link copied to clipboard
constructor(elementType: KClass<E>, universe: Array<Enum<E>>)

Types

Link copied to clipboard
object Companion

Throws InvalidObjectException.

Properties

Link copied to clipboard

The class of all the elements of this set.

Link copied to clipboard
expect abstract override val size: Int
Link copied to clipboard

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

Functions

Link copied to clipboard
abstract override fun add(element: E): Boolean
Link copied to clipboard
open override fun addAll(elements: Collection<E>): Boolean

abstract fun addAll()

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

Link copied to clipboard
abstract fun addRange(from: E, to: E)

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

Link copied to clipboard
expect open override fun clear()
Link copied to clipboard
open override fun clone(): EnumSet<E>

Returns a copy of this set.

Link copied to clipboard
abstract fun complement()

Complements the contents of this enum set.

Link copied to clipboard
open operator override fun contains(element: E): Boolean
Link copied to clipboard
open override fun containsAll(elements: Collection<E>): Boolean
Link copied to clipboard
expect open override fun isEmpty(): Boolean
Link copied to clipboard
abstract operator override fun iterator(): MutableIterator<E>
Link copied to clipboard
open override fun remove(element: E): Boolean
Link copied to clipboard
open override fun removeAll(elements: Collection<E>): Boolean
Link copied to clipboard
open override fun retainAll(elements: Collection<E>): Boolean