E
- the type of the elements of the multiset. If a multiset containing
heterogeneous types is needed, Object
can be used as type.public interface Multiset<E>
n
then the
element occurs n
times in the multiset.
As in classical sets, in a multiset the elements are not ordered and they are
not indexed.
It is forbidden to insert a null
value in a multiset.
Since the count is represented by an int
, a multiset can never
contains more than Integer.MAX_VALUE
occurrences for each element.Modifier and Type | Method and Description |
---|---|
void |
add(E element)
Adds a single occurrence of the specified element to this multiset.
|
int |
add(E element,
int occurrences)
Adds a number of occurrences of an element to this multiset.
|
void |
clear()
Removes all of the elements from this multiset.
|
boolean |
contains(java.lang.Object element)
Determines whether this multiset contains the specified element.
|
int |
count(java.lang.Object element)
Returns the number of occurrences of an element in this multiset.
|
java.util.Set<E> |
elementSet()
Returns the set of distinct elements contained in this multiset.
|
boolean |
equals(java.lang.Object object)
Compares the specified object with this multiset for equality.
|
int |
hashCode()
Returns the hash code for this multiset.
|
boolean |
isEmpty()
Returns true if this multiset contains no elements.
|
java.util.Iterator<E> |
iterator()
Returns an iterator for the multiset.
|
boolean |
remove(java.lang.Object element)
Removes a single occurrence of the specified element from this
multiset, if present.
|
int |
remove(java.lang.Object element,
int occurrences)
Removes a number of occurrences of the specified element from this
multiset.
|
int |
setCount(E element,
int occurrences)
Adds or removes the necessary occurrences of an element such that the
element attains the desired count.
|
int |
size()
Returns the total number of elements in this multiset counting all the
occurrences.
|
int size()
[1,2,3,1,4].size()
must return five by
counting the two occurrences of 1
.int count(java.lang.Object element)
element
- the element to count occurrences ofelement
in this
multiset; possibly zero but never negativejava.lang.NullPointerException
- if element
is nullint add(E element, int occurrences)
element
- the element to add occurrences ofoccurrences
- the number of occurrences of the element to add.
May be zero, in which case no change will be made.java.lang.IllegalArgumentException
- if occurrences
is negative,
or if this operation would result in
more than Integer.MAX_VALUE
occurrences of the elementjava.lang.NullPointerException
- if element
is null.void add(E element)
element
- the element to add one occurrence ofjava.lang.NullPointerException
- if element
is nulljava.lang.IllegalArgumentException
- if Integer.MAX_VALUE
occurrences of element
are
already contained in this multisetint remove(java.lang.Object element, int occurrences)
element
- the element to conditionally remove occurrences ofoccurrences
- the number of occurrences of the element to
remove. May be zero, in which case no change will
be made.java.lang.IllegalArgumentException
- if occurrences
is negativejava.lang.NullPointerException
- if element
is nullboolean remove(java.lang.Object element)
element
- the element to remove one occurrence oftrue
if an occurrence was found and removedjava.lang.NullPointerException
- if element
is nullint setCount(E element, int occurrences)
element
- the element to add or remove occurrences ofoccurrences
- the desired count of the element in this multisetjava.lang.IllegalArgumentException
- if count
is negativejava.lang.NullPointerException
- if element
is nulljava.util.Set<E> elementSet()
java.util.Iterator<E> iterator()
boolean contains(java.lang.Object element)
element
- the element to check fortrue
if this multiset contains at least one occurrence of
the elementjava.lang.NullPointerException
- if element
is nullvoid clear()
boolean isEmpty()
boolean equals(java.lang.Object object)
true
if the given object is also a multiset and contains equal
elements with equal counts, regardless of order.equals
in class java.lang.Object
int hashCode()
hashCode
in class java.lang.Object