E
- the type of elements maintained by this setpublic class HashMultiSet<E>
extends java.util.AbstractCollection<E>
The number of times an element is contained in the multi set is called the cardinality of the element.
This implementation uses a HashMap
to store the elements as keys, and
their cardinality as values. Like HashMap, it is not thread safe and supports
null
values. Unlike HashMap, the iterator does not support
removal of elements.
Constructor and Description |
---|
HashMultiSet(java.util.Collection<? extends E> contents)
Creates a new set containing all elements in the specified collection.
|
HashMultiSet(int capacity)
Creates an empty multiset with specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Adds the specified element to this set.
|
void |
clear()
Removes all occurrences of all elements from this set.
|
boolean |
contains(java.lang.Object o)
Returns
true if this set contains the specified element at
least once. |
boolean |
equals(java.lang.Object o)
Compares the specified object with this set for equality.
|
int |
flatSetSize()
Gets the number of distinct elements in this set, ignoring the
cardinality of the elements.
|
int |
getElementCount(java.lang.Object o)
Retrieves the cardinality of an element in this set.
|
java.util.Iterator<E> |
getFlatIterator()
Returns an iterator over the elements in the set, ignoring the
cardinality.
|
int |
hashCode()
Returns the hash code value for this set.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in the set.
|
boolean |
remove(java.lang.Object o)
Removes one occurrence of the specified element from this set.
|
boolean |
removeAll(java.util.Collection<?> c)
Removes elements in this set that are also contained in the specified
collection.
|
int |
removeAny(java.lang.Object o)
Removes all occurrences of an element from this set.
|
boolean |
retainAll(java.util.Collection<?> c)
Retains only the elements in this collection that are also contained in
the specified collection.
|
int |
size()
Returns the number of elements in the set, respecting cardinality of the
elements.
|
addAll, containsAll, isEmpty, toArray, toArray, toString
public HashMultiSet(int capacity)
capacity
- the initial capacitypublic HashMultiSet(java.util.Collection<? extends E> contents)
contents
- the collection whose elements are to be placed into this
setpublic java.util.Iterator<E> iterator()
N
will be returned N
times.
This iterator does not support Iterator.remove()
.
public java.util.Iterator<E> getFlatIterator()
This iterator does not support Iterator.remove()
.
public int size()
public int flatSetSize()
public boolean contains(java.lang.Object o)
true
if this set contains the specified element at
least once.public int getElementCount(java.lang.Object o)
o
- the element to look up0
if the
set does not contain the elementpublic boolean add(E e)
public boolean remove(java.lang.Object o)
public boolean removeAll(java.util.Collection<?> c)
Let the cardinality of element e
be n>0
in
this set, and assume the element is returned m≥0
times by
the iterator of the specified collection. After the operation,
m<n
, the cardinality of element e
in this
set is n-m
m≥n
, all occurrences of e
have been
removed from this setpublic boolean retainAll(java.util.Collection<?> c)
Let the cardinality of element e
be n>0
in
this set, and assume the element is returned m≥0
times by
the iterator of the specified collection. After the operation,
m<n
, the cardinality of element e
in this
set is m
m≥n
, the cardinality of element e
in this
set remains unchanged at n
public void clear()
public int removeAny(java.lang.Object o)
o
- The element to remove0
if
the set did not contain the elementpublic boolean equals(java.lang.Object o)
Note: if the specified object is not itself a HashMultiSet, we may have
this.equals(o)
but
this.hashCode()!=o.hashCode()
.
equals
in interface java.util.Collection<E>
equals
in class java.lang.Object
o
- object to be compared for equality with this setpublic int hashCode()
This implementation takes the sum of
hashCode(e)^cardinality(e)
over all elements in the set.
hashCode
in interface java.util.Collection<E>
hashCode
in class java.lang.Object