E
- the type of the elements of this enum setpublic class EnumSet<E extends Enum<E>> extends Object
EnumSet
class and is thus
a specialized MathSet
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.
Implementation note: All basic operations execute in constant time.
They are likely (though not guaranteed) to be much faster than their
MathSet
counterparts. Even bulk operations execute in
constant time if their argument is also an enum set.
For further technical details see EnumSet
.
Constructor and Description |
---|
EnumSet()
Constructs a new set.
|
EnumSet(Class<E> elementType)
Constructs a new, empty set.
|
EnumSet(Collection<E> c)
Creates an enum set initialized from the specified collection.
|
EnumSet(E element)
Constructs a new set containing the specified element.
|
EnumSet(E first,
E[] rest)
Constructs a new set from the input array.
|
EnumSet(EnumSet<E> enumSet)
Constructs a new set from the input enum set.
|
EnumSet(String enumName)
Constructs a new, empty set from the given name of an enum.
|
Modifier and Type | Method and Description |
---|---|
EnumSet<E> |
copy()
Creates and returns a clone copy of this set.
|
EnumSet<E> |
intersect(ArrayList<? extends EnumSet<? extends E>> sets)
Returns the intersection of this set and the specified set list.
|
EnumSet<E> |
intersect(EnumSet<? extends E> set)
Returns the intersection of this set and the specified set.
|
EnumSet<E> |
minus(E element)
Returns the set difference of this set minus the specified element.
|
EnumSet<E> |
minus(EnumSet<E> minuend)
Returns the set difference of this set minus the specified minuend.
|
static <E extends Enum<E>> |
partitions(EnumSet<E> set)
Returns a list of partitions of the specified set.
|
int |
size()
Returns the size of this EnumSet, that is, the number of its elements.
|
static <E extends Enum<E>> |
subsets(EnumSet<E> set,
int k)
Returns the k-element subsets of a set s,
i.e., each k-element combination of s,
stored in an array list.
|
ArrayList<EnumSet<E>> |
subsets(int k)
Returns the k-element subsets of this set,
i.e., each of its k-element combination, stored in an array list.
|
String |
toString()
Returns a string representation of this set.
|
EnumSet<E> |
unify(ArrayList<? extends EnumSet<? extends E>> sets)
Returns the union of this set and the specified set list.
|
EnumSet<E> |
unify(EnumSet<? extends E> set)
Returns the union of this set and the specified set.
|
public EnumSet()
null
object and therefore typeless.public EnumSet(Class<E> elementType)
EnumSet<Type> set = new EnumSet<Type>(Type.class);where
Type
is a given enum.elementType
- type of the elements of this setpublic EnumSet(String enumName) throws ClassNotFoundException
EnumSet set = new EnumSet(Name.class);where
Name
is the fully qualified name of a given enum.enumName
- the fully qualified name of the enumClassNotFoundException
- if the enum cannot be locatedpublic EnumSet(Collection<E> c)
copy()
.
Otherwise, the specified collection must contain at least one element
(in order to determine the underlying enum set's element type).c
- the collection from which this enum set is constructedpublic EnumSet(E element)
element
- the elementpublic EnumSet(E first, E[] rest)
EnumSet
.first
- the first elementrest
- array (varargs) containing the elements up to the first onepublic EnumSet<E> copy()
public EnumSet<E> minus(EnumSet<E> minuend)
minuend
- the set to be subtracted from this setpublic EnumSet<E> minus(E element)
element
- the set to be subtracted from this setpublic EnumSet<E> intersect(EnumSet<? extends E> set)
E
or a subclass (or implementing class) of E
.set
- a setpublic EnumSet<E> intersect(ArrayList<? extends EnumSet<? extends E>> sets)
EnumSet
or being of EnumSet
itself,
and each set is expected to contain elements of
class E
or a subclass (or implementing class) of E
.sets
- a list of setspublic EnumSet<E> unify(EnumSet<? extends E> set)
E
or a subclass (or implementing class) of E
.set
- a setpublic EnumSet<E> unify(ArrayList<? extends EnumSet<? extends E>> sets)
EnumSet
or being of EnumSet
itself,
and each set is expected to contain elements of
class E
or a subclass (or implementing class) of E
.sets
- a list of setspublic int size()
public ArrayList<EnumSet<E>> subsets(int k)
k
- an integerpublic String toString()
public static <E extends Enum<E>> ArrayList<EnumSet<E>> subsets(EnumSet<E> set, int k)
E
- type of the elements of this subsetset
- a setk
- an integerpublic static <E extends Enum<E>> ArrayList<MathSet<EnumSet<E>>> partitions(EnumSet<E> set)
{{a}, {b}, {c}},
{{a, b}, {c}}, {{a, c}, {b}}, {{a}, {c, b}},
{{a, b, c}}.
The running time of this algorithm with respect to the size n of the set is very bad, its time complexity is estimated as O(nn). For n ≤ 10 it requires less than 2 sec on a 2 GHz dual core processor, but for n ≤ 11 the running time is about 10 seconds and explodes for greater n.E
- the type of the elements of the setset
- a set