Interface MultiSet<T>
-
- Type Parameters:
T- the class of the elements of this multi-set.
- All Superinterfaces:
Iterable<T>
- All Known Subinterfaces:
SortedMultiSet<T>
- All Known Implementing Classes:
AbstractMultiSet,CollectionsExt.AbstractImmutableMultiSet,CollectionsExt.ImmutableMultiSet,CollectionsExt.ImmutableSortedMultiSet,HashMultiSet,TreeMultiSet
public interface MultiSet<T> extends Iterable<T>
Represents a set with multiplicities. Mathematically this is something between a set and a family. Note that implementations of this kind of set need not supportnullelements.Allows also to create an immutable
MultiSeteither from a set or as a copy of anotherMultiSet.Note that this should extend Collection, but still does not *****. maybe it should even extend Set.
- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceMultiSet.MultiplicityRepresents the multiplicity of an entry of the enclosing multi-set.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description booleanadd(T obj)Addsobjto thisMultiSet.booleanaddAll(MultiSet<? extends T> mvs)Addsmvselementwise to this multi set taking multiplicities into account and returns whether this caused a change of the underlying set. **** strange implementation; also: changebooleanaddAll(Set<? extends T> set)Addssetelementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: changeintaddWithMult(T obj)Addsobjto thisMultiSetand returns the new multiplicity of this object.intaddWithMult(T obj, int addMult)Increases the multiplicity ofobjin thisMultiSetby the specified valueaddMultand returns the new multiplicity of this object.voidclear()Removes all of the elements from thisMultiSet.booleancontains(Object obj)Returnstrueif thisMultiSetcontains the specified element.booleancontainsAll(Collection<?> coll)Returnstrueif thisMultiSetcontains all of the elements in the specified collection with strictly positive multiplicity.booleanequals(Object obj)Returnstrueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.Map<T,MultiSet.Multiplicity>getMap()Returns a view of the underlying map of thisMultiSetas a map mapping each entry to its multiplicity.intgetMaxMult()Returns the maximal multiplicity of an element in this set.intgetMultiplicity(Object obj)Returns the multiplicity with which the given object occurs within this set.MultiSet.MultiplicitygetMultiplicityObj(Object obj)Returns the multiplicity object of the given object in this set ornull.ObjectgetObjWithMaxMult()Returns one of the elements in this multiple set with maximal multiplicity.Set<T>getSet()Returns a view of the underlying set of thisMultiSet.Set<Map.Entry<T,MultiSet.Multiplicity>>getSetWithMults()Returns a Set view of the mapping from the element of thisMultiSetto the according multiplicities.inthashCode()booleanisEmpty()Returns whether this multiple set contains no element.MultiSetIterator<T>iterator()Returns an iterator over the elements in this collection which emits each element exactly once, without regarding its multiplicity.booleanremove(Object obj)Removes all instances of the specified element from thisMultiSet, if it is present with nontrivial multiplicity.booleanremoveAll(Collection<?> coll)Removes all thisMultiSet's elements that are also contained in the specified collection.intremoveWithMult(Object obj)Decrements the multiplicity ofobjin thisMultiSetif it is present and returns the old multiplicity ofobj; If this is0returns without altering thisMultiSet.intremoveWithMult(Object obj, int removeMult)Decreases the multiplicity ofobjin thisMultiSetby the specified valueremoveMultif possible and returns the old multiplicity ofobj.booleanretainAll(Collection<?> coll)Retains only the elements in thisMultiSetthat are contained in the specified collection.intsetMultiplicity(T obj, int newMult)Sets the multiplicity ofobjto the value specified bymult.intsize()Returns the number of pairwise different elements in thisMultiSet.intsizeWithMult()Returns the number of elements in thisMultiSetcounted with multiplicities.Object[]toArray()Returns an array containing all of the elements in thisMultiSetexactly once, ignoring its multiplicity.T[]toArray(T[] arr)Returns an array containing all of the elements in thisMultiSet; the runtime type of the returned array is that of the specified array.StringtoString()-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
size
int size()
Returns the number of pairwise different elements in thisMultiSet. If thisMultiSetcontains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Returns:
- the number of elements in this
MultiSeteach multiple element counted as a single one. - See Also:
sizeWithMult()
-
sizeWithMult
int sizeWithMult()
Returns the number of elements in thisMultiSetcounted with multiplicities. If thisMultiSetcontains more thanInteger.MAX_VALUEelements, returnsInteger.MAX_VALUE.- Returns:
- the number of elements in this
MultiSetcounted with multiplicities, provided this does not exceedInteger.MAX_VALUE; otherwise justInteger.MAX_VALUE. - See Also:
size()
-
isEmpty
boolean isEmpty()
Returns whether this multiple set contains no element.- Returns:
- whether this multiple set contains no element.
-
getObjWithMaxMult
Object getObjWithMaxMult()
Returns one of the elements in this multiple set with maximal multiplicity. The return value isnullif and only if this set is empty.- Returns:
- a
Object o != nullwith maximal multiplicity ornullif this multiple set is empty. - See Also:
isEmpty()
-
getMaxMult
int getMaxMult()
Returns the maximal multiplicity of an element in this set. In particular for empty sets returns0.- Returns:
- a non-negative
intvalue which is the maximal mutliplicity of an element in this set. In particular this is0if and only if this set is empty.
-
getMultiplicity
int getMultiplicity(Object obj)
Returns the multiplicity with which the given object occurs within this set.- Parameters:
obj- anObjectand not null.- Returns:
- a non-negative
intvalue which is the mutliplicity of the given element in this set. In particular this is0if and only ifobjis an instance which is not in this set. - Throws:
NullPointerException- forobj==null.- See Also:
setMultiplicity(Object, int),getMultiplicityObj(Object)
-
getMultiplicityObj
MultiSet.Multiplicity getMultiplicityObj(Object obj)
Returns the multiplicity object of the given object in this set ornull.- Parameters:
obj- anObjectand not null.- Returns:
- If
objis an instance which is in this set, a multiplicity object wrapping the multiplicity is returned. Ifobjis an instance which is not in this set,nullis returned. - Throws:
NullPointerException- forobj==null.- See Also:
getMultiplicity(Object)
-
contains
boolean contains(Object obj)
Returnstrueif thisMultiSetcontains the specified element. More formally, returnstrueif and only if thisMultiSetcontains at least one elementesuch that(o==null ? e==null : o.equals(e)).- Parameters:
obj- element (notnull) whose presence in thisMultiSetis to be tested.- Returns:
trueif thisMultiSetcontains the specified element.- Throws:
NullPointerException- forobj==null.
-
iterator
MultiSetIterator<T> iterator()
Returns an iterator over the elements in this collection which emits each element exactly once, without regarding its multiplicity. For certain implementations, the iterator returned does not allow modifications of the underlying (multi-)set.
-
toArray
Object[] toArray()
Returns an array containing all of the elements in thisMultiSetexactly once, ignoring its multiplicity.The returned array will be "safe" in that no references to it are maintained by this
MultiSet. (In other words, this method must allocate a new array even if thisMultiSetis backed by an array). The caller is thus free to modify the returned array.- Returns:
- an array containing all of the elements in this collection
- See Also:
iterator()
-
toArray
T[] toArray(T[] arr)
Returns an array containing all of the elements in thisMultiSet; the runtime type of the returned array is that of the specified array. If theMultiSetfits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of thisMultiSet.If this
MultiSetfits in the specified array with room to spare (i.e., the array has more elements than thisMultiSet), the elementin the array immediately following the end of theMultiSetis set tonull. This is useful in determining the length of thisMultiSetbecause thisMultiSetdoes not contain anynullelements.Suppose
lis aListknown to contain only strings. The following code can be used to dump the list into a newly allocated array ofString:String[] x = (String[]) v.toArray(new String[0]);Note that
toArray(new Object[0])is identical in function totoArray().- Parameters:
arr- the array into which the elements of thisMultiSetare to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing each elements of this
MultiSetexactly once. - Throws:
ArrayStoreException- the runtime type of the specified array is not a supertype of the runtime type of every element in thisMultiSet.NullPointerException- if the specified array isnull.
-
addWithMult
int addWithMult(T obj)
Addsobjto thisMultiSetand returns the new multiplicity of this object. In other words, increments the multiplicity ofobjby one. This is a special case ofaddWithMult(Object obj, int addMult)withaddMult==1.- Parameters:
obj- aObject. Note that this object may not benull.- Returns:
- a strictly positive
intvalue: the new multiplicity ofobj. - Throws:
NullPointerException- if the specified element is null.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
addWithMult
int addWithMult(T obj, int addMult)
Increases the multiplicity ofobjin thisMultiSetby the specified valueaddMultand returns the new multiplicity of this object. This generalizesaddWithMult(Object obj)withaddMult==1.- Parameters:
obj- anObjectinstance.addMult- a non-negative integer specifying the multiplicity with whichobjis to be added.- Returns:
- a non-negative
intvalue: the new multiplicity ofobj. - Throws:
IllegalArgumentException- foraddMult < 0.NullPointerException- forobj==nullprovidedaddMult ≥ 0.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
add
boolean add(T obj)
Addsobjto thisMultiSet. In other words, increments the multiplicity ofobjby one. Returnstrueif thisMultiSetinterpreted as a set changed as a result of the call. (Returnsfalseif thisMultiSetalready contains the specified element (with nontrivial multiplicity).- Parameters:
obj- element the multiplicity of which in thisMultiSetis to be increased by one. Note that this may not benull.- Returns:
trueif and only if the multiplicity of the specified element was0before the call of this method.- Throws:
NullPointerException- if the specified element isnull.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
removeWithMult
int removeWithMult(Object obj)
Decrements the multiplicity ofobjin thisMultiSetif it is present and returns the old multiplicity ofobj; If this is0returns without altering thisMultiSet.- Parameters:
obj- aObject. Note that this object may not benull.- Returns:
- a non-negative
intvalue: the old multiplicity ofobjbefore a potential modification of thisMultiSet. - Throws:
NullPointerException- if the specified element is null.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
removeWithMult
int removeWithMult(Object obj, int removeMult)
Decreases the multiplicity ofobjin thisMultiSetby the specified valueremoveMultif possible and returns the old multiplicity ofobj.- Parameters:
obj- anObjectinstance.removeMult- a non-negative integer specifying the multiplicity with whichobjis to be removed.- Returns:
- a non-negative
intvalue: the old multiplicity ofobjbefore a potential modification of thisMultiSet. - Throws:
NullPointerException- forobj == null.IllegalArgumentException- forremoveMult < 0and also ifremoveMult - obj.getMultiplicity() < 0.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
remove
boolean remove(Object obj)
Removes all instances of the specified element from thisMultiSet, if it is present with nontrivial multiplicity. More formally, immediately after having (successively) invokeds.remove(o), the conditions.contains(o) == falseis satisfied. Returns true if thisMultiSetcontained the specified element (or equivalently, if (the underlying set of) thisMultiSetchanged as a result of the call).- Parameters:
obj- element the multiplicity of which in thisMultiSetis to be increased by one.- Returns:
trueif and only if thisMultiSetchanged as a result of the call.- Throws:
NullPointerException- if the specified element isnull.UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
setMultiplicity
int setMultiplicity(T obj, int newMult)
Sets the multiplicity ofobjto the value specified bymult.- Parameters:
obj- anObjectinstance.newMult- a non-negativeintvalue.- Returns:
- the old multiplicity of
objas a non-negativeintvalue. - Throws:
IllegalArgumentException- if eitherobj == nullormult ≤ 0.UnsupportedOperationException- if thisMultiSetdoes not support this method.- See Also:
getMultiplicity(Object)
-
containsAll
boolean containsAll(Collection<?> coll)
Returnstrueif thisMultiSetcontains all of the elements in the specified collection with strictly positive multiplicity.- Parameters:
coll- collection to be checked for containment in thisMultiSet.- Returns:
trueif thisMultiSetcontains all of the elements in the specified collection.- Throws:
NullPointerException- if the specified collection contains one or more null elements.NullPointerException- if the specified collection isnull.- See Also:
contains(Object)
-
addAll
boolean addAll(MultiSet<? extends T> mvs)
Addsmvselementwise to this multi set taking multiplicities into account and returns whether this caused a change of the underlying set. **** strange implementation; also: change- Parameters:
mvs- aMultiSetobject.- Returns:
- returns whether adding changed this
MultiSetinterpreted as a set. - Throws:
UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
addAll
boolean addAll(Set<? extends T> set)
Addssetelementwise to this multi set increasing multiplicities and returns whether this caused a change of the underlying set. **** strange implementation; also: change- Parameters:
set- aSetobject.- Returns:
- returns whether adding changed this
MultiSetinterpreted as a set. - Throws:
UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
removeAll
boolean removeAll(Collection<?> coll)
Removes all thisMultiSet's elements that are also contained in the specified collection. After this call returns, thisMultiSetwill contain no elements in common with the specified collection.- Parameters:
coll- elements to be removed from thisMultiSet.- Returns:
trueif thisMultiSetchanged as a result of the call.- Throws:
NullPointerException- if the specified collection isnull.UnsupportedOperationException- if thisMultiSetdoes not support this method.- See Also:
remove(Object),contains(Object)
-
retainAll
boolean retainAll(Collection<?> coll)
Retains only the elements in thisMultiSetthat are contained in the specified collection. In other words, removes from thisMultiSetall of its elements that are not contained in the specified collection.- Parameters:
coll- elements to be retained in thisMultiSet.- Returns:
trueif thisMultiSetchanged as a result of the call.- Throws:
NullPointerException- if the specified collection isnull.UnsupportedOperationException- if thisMultiSetdoes not support this method.- See Also:
remove(Object),contains(Object)
-
clear
void clear()
Removes all of the elements from thisMultiSet. ThisMultiSetwill be empty after this method returns.- Throws:
UnsupportedOperationException- if thisMultiSetdoes not support this method.
-
getSet
Set<T> getSet()
Returns a view of the underlying set of thisMultiSet. For certain implementations, this set is immutable to prevent implicit modification of thisMultiSet.- Returns:
- the
Setcontaining exactly the objects with strictly positive multiplicity in thisMultiSet.
-
getMap
Map<T,MultiSet.Multiplicity> getMap()
Returns a view of the underlying map of thisMultiSetas a map mapping each entry to its multiplicity.
-
getSetWithMults
Set<Map.Entry<T,MultiSet.Multiplicity>> getSetWithMults()
Returns a Set view of the mapping from the element of thisMultiSetto the according multiplicities. The set is backed by theMultiSet, so changes to the map are reflected in the set, and vice-versa. If theMultiSetis modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set may support element removal, which removes the corresponding element from theMultiSet, via theIterator.remove(),Set.remove(Object),Set.removeAll(Collection),Set.retainAll(Collection)andclear()operations. It does not support the methodsadd(Object)orSet.addAll(Collection).
-
equals
boolean equals(Object obj)
Returnstrueif and only ifobjis also aMultiSetand contains the same elements with the same multiplicities as this one.
-
-