E
- the type of the elements of this setpublic class MathSet<E> extends HashSet<E>
HashSet
class and is backed
by a hash table (actually a HashMap
instance).
It makes no guarantees as to the iteration order of the set; in particular,
it does not guarantee that the order will remain constant over time.
This class permits the null element.
This class offers constant time performance for the basic operations
(add, remove, contains and size), assuming the hash function disperses the
elements properly among the buckets. Iterating over this set requires time
proportional to the sum of the HashSet instance's size (the number of elements)
plus the "capacity" of the backing HashMap instance (the number of buckets).
Thus, it is very important not to set the initial capacity too high
(or the load factor too low) if iteration performance is important.
For further technical details see HashSet
.OrderedSet
,
HashSet
,
Serialized FormConstructor and Description |
---|
MathSet()
Constructs a new, empty set; the backing HashMap instance has default
initial capacity (16) and load factor (0.75).
|
MathSet(Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
|
MathSet(E element)
Constructs a new set from the input element.
|
MathSet(E[] elements)
Constructs a new set from the input array.
|
MathSet(int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the
specified initial capacity and default load factor (0.75).
|
MathSet(int initialCapacity,
float loadFactor)
Constructs a new, empty set; the backing HashMap instance has the
specified initial capacity and the specified load factor.
|
Modifier and Type | Method and Description |
---|---|
MathSet<E> |
copy()
Creates and returns a clone copy of this set.
|
static <E> MathSet<E> |
emptySet()
Returns the empty set.
|
static <E> MathSet<E> |
emptySet(MathSet<E> set)
Returns the empty set.
|
MathSet<E> |
intersect(ArrayList<? extends Set<? extends E>> sets)
Returns the intersection of this set and the specified set list.
|
MathSet<E> |
intersect(Set<? extends E> set)
Returns the intersection of this set and the specified set.
|
MathSet<E> |
minus(E element)
Returns the set difference of this set minus the singleton generated by the
specified element.
|
MathSet<E> |
minus(Set<E> minuend)
Returns the set difference of this set minus the specified minuend.
|
ArrayList<MathSet<MathSet<E>>> |
partitions()
Returns a list of partitions of this set.
|
static <E> ArrayList<int[]> |
partitions(E[] set)
Returns a list of partitions of the specified set,
each partition encoded as an array
partition[] meaning that
set element i is in subset partition[i] . |
static <E> ArrayList<MathSet<MathSet<E>>> |
partitions(MathSet<E> set)
Returns a list of partitions of the specified set.
|
ArrayList<MathSet<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.
|
static <E> ArrayList<MathSet<E>> |
subsets(Set<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.
|
String |
toString() |
MathSet<E> |
unify(ArrayList<? extends Set<? extends E>> sets)
Returns the union of this set and the specified set list.
|
MathSet<E> |
unify(Set<? extends E> set)
Returns the union of this set and the specified set.
|
add, clear, clone, contains, isEmpty, iterator, remove, size, spliterator
equals, hashCode, removeAll
addAll, containsAll, retainAll, toArray, toArray
finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
parallelStream, removeIf, stream
public MathSet()
public MathSet(Collection<? extends E> c)
c
- a collectionpublic MathSet(int initialCapacity)
initialCapacity
- the initial capacity of the hash mapIllegalArgumentException
- - if the initial capacity is less than zeropublic MathSet(int initialCapacity, float loadFactor)
initialCapacity
- the initial capacity of the hash maploadFactor
- - the load factor of the backing hash mapIllegalArgumentException
- if the initial capacity is less than zero,
or if the load factor is nonpositivepublic MathSet(E[] elements)
Set
.
The backing HashMap instance has the
initial capacity of the array size and the default load factor (0.75).
Note that for elements of primitive type you must invoke the wrapper class
objects, e.g., MathSet<Integer> s = new MathSet<>(new Integer[]{1,2,3});
.elements
- array containing the elementspublic MathSet(E element)
MathSet<Integer> s = new MathSet<>(new Integer(2));
.element
- an elementpublic MathSet<E> copy()
public MathSet<E> minus(Set<E> minuend)
minuend
- the set to be subtracted from this setpublic MathSet<E> minus(E element)
element
- specifying the singleton set to be subtracted from this setpublic MathSet<E> intersect(Set<? extends E> set)
E
or a subclass (or implementing class) of E
.set
- a setpublic MathSet<E> intersect(ArrayList<? extends Set<? extends E>> sets)
MathSet
or being of MathSet
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 MathSet<E> unify(Set<? extends E> set)
E
or a subclass (or implementing class) of E
.set
- a setpublic MathSet<E> unify(ArrayList<? extends Set<? extends E>> sets)
MathSet
or being of MathSet
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 ArrayList<MathSet<E>> subsets(int k)
Combinatorics.subsets(java.util.SortedSet,int)
for sorted sets.k
- an integerCombinatorics.subsets(java.util.SortedSet, int)
public ArrayList<MathSet<MathSet<E>>> partitions()
{{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. The number of partitions of a set of n is given by the n-th Bell number Bn, see http://oeis.org/A000110.public static <E> MathSet<E> emptySet()
MathSet<String> s = MathSet.emptySet();Implementation note: Implementations of this method need not create a separate MathSet object for each call. If an explicit variable for the empty set is not desired, the method
emptySet(MathSet<E>)
may be used.E
- type of the elements of this setemptySet(MathSet)
,
Collections.emptySet()
public static <E> MathSet<E> emptySet(MathSet<E> set)
MathSet.emptySet(new MathSet<String>());This method is appropriate if an explicit variable for the empty set is not desired.
E
- type of the elements of this setset
- specifies the element types of this empty setemptySet()
public static <E> ArrayList<MathSet<E>> subsets(Set<E> set, int k)
E
- type of the elements of this setset
- a setk
- an integerpublic static <E> ArrayList<MathSet<MathSet<E>>> partitions(MathSet<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 ≤ 12 the running time is about 10 seconds and explodes for greater n. The number of partitions of a set of n is given by the n-th Bell number Bn, see http://oeis.org/A000110.E
- the type of the elements of the setset
- a setpublic static <E> ArrayList<int[]> partitions(E[] set)
partition[]
meaning that
set element i
is in subset partition[i]
.
In general, a partition of a set is a collection of disjoint subsets whose union equals
the set. For instance, the set S = {a, b, c}
has the five partitions
{{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 ≤ 12 the running time is about 10 seconds and explodes for greater n. The number of partitions of a set of n is given by the n-th Bell number Bn, see http://oeis.org/A000110.E
- the type of the elements of the setset
- a setpartitions(MathSet)
public String toString()
toString
in class AbstractCollection<E>