|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.AbstractCollection
mathCollection.AbstractMultiset
mathCollection.HashMultiset
This class implements the Multiset
interface, backed by a hash
map (specifically, a HashMap
instance).
Equal elements are returned in successive order, whereas different
elements have no specific iteration order; in particular, it is not
guaranteed that the element order will remain constant over time.
Multiple copies of the same element only require as much memory as a
single instance of it. This class permits the null
element.
Note that this implementation is not synchronized. If multiple
threads access a set concurrently, and at least one of the threads modifies
the set, it must be synchronized externally. This is typically
accomplished by synchronizing on some object that naturally encapsulates
the set. If no such object exists, the set should be "wrapped" using the
MathCollections.synchronizedMultiset
method. This is best done
at creation time, to prevent accidental unsynchronized access to the
HashMultiset
instance:
Multiset mus = MathCollections.synchronizedMultiset(new HashMultiset(...));
Collection
,
Multiset
,
AbstractMultiset
,
HashMap
,
Serialized FormNested Class Summary | |
private class |
HashMultiset.HashMultisetIterator
An iterator that, in spite of the specific element storage technique in a Multiset (equal elements get 'counted' instead of
each being stored separately), iterates over individual
Multiset elements. |
Field Summary | |
private boolean |
isConcurrentlyModified
Used to check, whether this multiset was modified by an destructive method while iterating over it. |
private java.util.HashMap |
myHashMap
The backing instance of HashMap where the elements of this
set are stored. |
private int |
storedHashCode
Acts as a cache for the hash code value of this set out of performance considerations. |
private int |
storedSize
Acts as a cache for the size value of this set out of performance considerations. |
Constructor Summary | |
HashMultiset()
Constructs a new, empty multiset; the backing HashMap
instance has default initial capacity (16) and load factor (0.75). |
|
HashMultiset(java.util.Collection c)
Constructs a new multiset containing the elements in the specified collection. |
|
HashMultiset(int initialCapacity)
Constructs a new, empty multiset; the backing HashMap
instance has specified initial capacity and default load factor
(0.75). |
|
HashMultiset(int initialCapacity,
float loadFactor)
Constructs a new, empty multiset; the backing HashMap
instance has specified initial capacity and load factor. |
Method Summary | |
boolean |
add(java.lang.Object o)
Adds the specified element to this multiset. |
boolean |
add(java.lang.Object o,
int quantity)
Adds the specified element quantity of times to this
multiset. |
void |
clear()
Removes all elements from this set. |
java.lang.Object |
clone()
Returns a shallow copy of this HashMultiset instance: the
elements themselves are not cloned. |
boolean |
contains(java.lang.Object o)
Returns true if this set contains the specified element. |
Multiset |
difference(java.util.Collection c)
Returns the asymmetric difference between this multiset and the specified collection. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this multiset for equality. |
int |
getQuantity(java.lang.Object o)
Returns the number of times the specified element is present in this multiset. |
int |
hashCode()
Returns the hash code value for this multiset. |
Multiset |
intersection(java.util.Collection c)
Returns the intersection with the specified collection. |
boolean |
isEmpty()
Returns true if this multiset contains no elements. |
boolean |
isSuperset(java.util.Collection c)
Returns true if this multiset is a superset of the
specified collection. |
java.util.Iterator |
iterator()
Returns an iterator over the elements in this multiset. |
boolean |
remove(java.lang.Object o)
Removes the specified element from this multiset if it is present. |
boolean |
remove(java.lang.Object o,
int quantity)
Removes the specified element quantity of times from this
multiset if possible. |
boolean |
setQuantity(java.lang.Object o,
int quantity)
Adjusts the number of times the specified element is present in this multiset to be the specified value (zero if the value is negative). |
int |
setSize()
Returns the size of a 'flattened' version of this multiset in which every element of this multiset is present exactly once. |
int |
size()
Returns the number of elements in this multiset (its cardinality). |
Multiset |
sum(java.util.Collection c)
Returns the sum with the specified collection. |
Multiset |
symmetricDifference(java.util.Collection c)
Returns the symmetric difference between this multiset and the specified collection. |
java.util.Set |
toSet()
Returns a new Set containing the 'flattened' version of
this multiset in which every element of this multiset is present exactly
once. |
Multiset |
union(java.util.Collection c)
Returns the union with the specified collection. |
Methods inherited from class mathCollection.AbstractMultiset |
isDisjoint, isSubset, toString |
Methods inherited from class java.util.AbstractCollection |
addAll, containsAll, removeAll, retainAll, toArray, toArray |
Methods inherited from class java.lang.Object |
finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface mathCollection.Multiset |
isDisjoint, isSubset |
Methods inherited from interface java.util.Collection |
addAll, containsAll, removeAll, retainAll, toArray, toArray |
Field Detail |
private java.util.HashMap myHashMap
HashMap
where the elements of this
set are stored.
private int storedHashCode
hashCode()
method is called.
private int storedSize
private boolean isConcurrentlyModified
Constructor Detail |
public HashMultiset()
HashMap
instance has default initial capacity (16) and load factor (0.75).
public HashMultiset(java.util.Collection c)
HashMap
instance is created with
default load factor (0.75) and an initial capacity sufficient to contain
the elements in the specified collection.
c
- the collection whose elements are to be placed into this
multiset.
java.lang.NullPointerException
- if the specified collection is null.public HashMultiset(int initialCapacity)
HashMap
instance has specified initial capacity and default load factor
(0.75). Note that the backing HashMap
only stores single
copies of equal elements.
initialCapacity
- the initial capacity for distinct
elements.
java.lang.IllegalArgumentException
- if the initial capacity is negative.public HashMultiset(int initialCapacity, float loadFactor)
HashMap
instance has specified initial capacity and load factor. Note that the
backing HashMap
only stores single copies of equal elements.
initialCapacity
- the initial capacity for distinct
elements.loadFactor
- the load factor.
java.lang.IllegalArgumentException
- if the initial capacity is negative
or the load factor is nonpositive.Method Detail |
public java.util.Iterator iterator()
iterator
in interface java.util.Collection
ConcurrentModificationException
public int hashCode()
s1.equals(s2)
implies that
s1.hashCode()==s2.hashCode()
for any two multisets
s1
and s2
, as required by the general contract
of Object.hashCode()
.
This implementation first checks whether a cached hash code value is
available. If not (i.e. storedHashCode
is zero), the hash
code gets calculated using hashCode()
of the super class.
hashCode
in interface java.util.Collection
hashCode
in class AbstractMultiset
public java.lang.Object clone()
HashMultiset
instance: the
elements themselves are not cloned.
public boolean isEmpty()
true
if this multiset contains no elements.
isEmpty
in interface java.util.Collection
true
if this multiset contains no elements,
false
otherwise.public int size()
size
in interface java.util.Collection
public void clear()
clear
in interface java.util.Collection
public boolean contains(java.lang.Object o)
true
if this set contains the specified element.
contains
in interface java.util.Collection
o
- element whose presence in this set is to be tested.
true
if this set contains the specified element,
false
otherwise.public boolean add(java.lang.Object o)
This implementation sets storedHashCode
to 0 (representing
an unavailable hash code value), which forces hashCode()
to
recalculate the actual hash code value.
add
in interface java.util.Collection
o
- element to be added to this set.
true
, adding an element to a multiset will always
be a success.public boolean add(java.lang.Object o, int quantity)
quantity
of times to this
multiset. If quantity
is negative or 0, the multiset
remains unchanged and false
is returned.
If the set gets altered, this implementation sets
storedHashCode
to 0 (representing an unavailable hash code
value), which forces hashCode()
to recalculate the actual
hash code value.
add
in interface Multiset
o
- element to be added to this set.quantity
- quantity of elements to add.
true
if quantity
is
greater than 0, false
otherwisepublic boolean remove(java.lang.Object o)
If the set gets altered, this implementation sets
storedHashCode
to 0 (representing an unavailable hash code
value), which forces hashCode()
to recalculate the actual
hash code value.
remove
in interface java.util.Collection
o
- object to be removed from this multiset, if present.
true
if the multiset contained the specified
element, false
otherwise.public boolean remove(java.lang.Object o, int quantity)
quantity
of times from this
multiset if possible. If quantity
is negative or 0, the
multiset remains unchanged and false
is returned.
If the set gets altered, this implementation sets
storedHashCode
to 0 (representing an unavailable hash code
value), which forces hashCode()
to recalculate the actual
hash code value.
remove
in interface Multiset
o
- object to be removed from this multiset, if present.quantity
- quantity of elements to remove.
true
if the multiset got altered,
false
otherwise.public boolean equals(java.lang.Object o)
true
if the specified object is also a multiset, the two
sets have the same size, and every element of the specified set is
contained in this set the same number of times.
This implementation first checks if the given object is a
HashMultiset
. If so, the hash code values of this multiset
and the specified HashMultiset
are compared.
Since the hash code values are being cached, this represents a quick
solution if the sets aren't equal. However, if the hash code values are
equal, it cannot be assumed that the sets themselves are equal,
since different sets can have the same hash code value. In this case,
the sets are compared on a per-element basis using the super method
equals
.
equals
in interface java.util.Collection
equals
in class AbstractMultiset
o
- object to be compared for equality with this multiset.
true
if the specified object is equal to this
multiset, false
otherwise.public int getQuantity(java.lang.Object o)
getQuantity
in interface Multiset
o
- element whose quantity is returned.
setQuantity(java.lang.Object, int)
public boolean setQuantity(java.lang.Object o, int quantity)
This implementation sets storedHashCode
to 0 (representing
an unavailable hash code value), which forces hashCode()
to
recalculate the actual hash code value.
setQuantity
in interface Multiset
o
- element whose quantity gets set.quantity
- quantity of the specified element to be set.
true
if this multiset has been modified,
false
otherwise.getQuantity(java.lang.Object)
public java.util.Set toSet()
Set
containing the 'flattened' version of
this multiset in which every element of this multiset is present exactly
once.
toSet
in interface Multiset
public int setSize()
setSize
in interface Multiset
public boolean isSuperset(java.util.Collection c)
true
if this multiset is a superset of the
specified collection. That is, if all elements of the specified
collection are also present in this multiset at least the same number of
times.
This implementation checks if the specified collection is an instance of
Multiset
or Set
. If so, the result of the
super method isSuperset
is returned. Otherwise, it tries
to create the intersection of this HashMultiset and the specified
Collection c by iterating over c and adding common elements to a new
multiset. If an element is found whose quantity in the current
intersection multiset is greater or equal than in this HashMultiset,
false is returned. If the intersection can be built up completely, this
HashMultiset is a superset of c and true is returned.
isSuperset
in interface Multiset
isSuperset
in class AbstractMultiset
c
- collection to be checked for being a subset.
true
if this multiset is a superset of the
specifed collection, false
otherwise.AbstractMultiset.isSubset(Collection)
public Multiset sum(java.util.Collection c)
Multiset
containing all elements that are present in this
multiset or in the specified collection. The quantities of equal
elements get added up.
sum
in interface Multiset
c
- collection to be united with.
public Multiset union(java.util.Collection c)
HashMultiset
containing all elements that are present in
this multiset or in the specified collection. For equal elements, the
resulting quantity is the maximum of the two given quantities.
union
in interface Multiset
c
- collection to be united with.
public Multiset intersection(java.util.Collection c)
HashMultiset
containing all elements that are present in
this multiset as well as in the specified collection. For equal elements,
the resulting quantity is the minimum of the two given quantities.
intersection
in interface Multiset
c
- collection to be intersected with.
public Multiset difference(java.util.Collection c)
HashMultiset
containing
all elements that are present in this multiset but not in the specified
collection. The quantities of equal elements get subtracted.
difference
in interface Multiset
c
- collection from which the difference is calculated.
public Multiset symmetricDifference(java.util.Collection c)
HashMultiset
containing
all elements that are present either in this multiset or in the specified
collection but not in both. The quantities of equal elements get
subtracted from each other (maximum minus minimum).
symmetricDifference
in interface Multiset
c
- collection from which the symmetric difference is calculated
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |