E
- the type held in the multisetpublic abstract class AbstractMultiSet<E> extends AbstractCollection<E> implements MultiSet<E>
MultiSet
interface to simplify the
creation of subclass implementations.Modifier and Type | Class and Description |
---|---|
protected static class |
AbstractMultiSet.AbstractEntry<E>
Inner class AbstractEntry.
|
protected static class |
AbstractMultiSet.EntrySet<E>
Inner class EntrySet.
|
protected static class |
AbstractMultiSet.UniqueSet<E>
Inner class UniqueSet.
|
MultiSet.Entry<E>
Modifier | Constructor and Description |
---|---|
protected |
AbstractMultiSet()
Constructor needed for subclass serialisation.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds one copy of the specified object to the MultiSet.
|
int |
add(E object,
int occurrences)
Adds a number of occurrences of the specified object to the MultiSet.
|
void |
clear()
Clears the multiset removing all elements from the entrySet.
|
boolean |
contains(Object object)
Determines if the multiset contains the given element.
|
protected Set<MultiSet.Entry<E>> |
createEntrySet()
Create a new view for the set of entries in this multiset.
|
protected abstract Iterator<MultiSet.Entry<E>> |
createEntrySetIterator()
Creates an entry set iterator.
|
protected Set<E> |
createUniqueSet()
Create a new view for the set of unique elements in this multiset.
|
protected Iterator<E> |
createUniqueSetIterator()
Creates a unique set iterator.
|
protected void |
doReadObject(ObjectInputStream in)
Read the multiset in using a custom routine.
|
protected void |
doWriteObject(ObjectOutputStream out)
Write the multiset out using a custom routine.
|
Set<MultiSet.Entry<E>> |
entrySet()
Returns an unmodifiable view of the entries of this multiset.
|
boolean |
equals(Object object)
Compares this MultiSet to another object.
|
int |
getCount(Object object)
Returns the number of occurrence of the given element in this multiset by
iterating over its entrySet.
|
int |
hashCode()
Gets a hash code for the MultiSet compatible with the definition of equals.
|
Iterator<E> |
iterator()
Gets an iterator over the multiset elements.
|
boolean |
remove(Object object)
Removes one occurrence of the given object from the MultiSet.
|
int |
remove(Object object,
int occurrences)
Removes a number of occurrences of the specified object from the MultiSet.
|
boolean |
removeAll(Collection<?> coll)
Remove all occurrences of all elements from this MultiSet represented
in the given collection.
|
int |
setCount(E object,
int count)
Sets the number of occurrences of the specified object in the MultiSet
to the given count.
|
int |
size()
Returns the number of elements in this multiset.
|
String |
toString()
Implement a toString() method suitable for debugging.
|
protected abstract int |
uniqueElements()
Returns the number of unique elements in this multiset.
|
Set<E> |
uniqueSet()
Returns a view of the unique elements of this multiset.
|
addAll, containsAll, isEmpty, retainAll, toArray, toArray
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, retainAll
addAll, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray
protected AbstractMultiSet()
public int size()
size
in interface Collection<E>
size
in interface MultiSet<E>
size
in class AbstractCollection<E>
public int getCount(Object object)
public int setCount(E object, int count)
MultiSet
If the provided count is zero, the object will be removed from the
MultiSet.uniqueSet()
.
public boolean contains(Object object)
contains
in interface Collection<E>
contains
in class AbstractCollection<E>
object
- the object to search forpublic Iterator<E> iterator()
public boolean add(E object)
MultiSet
If the object is already in the MultiSet.uniqueSet()
then increment its
count as reported by MultiSet.getCount(Object)
. Otherwise add it to the
MultiSet.uniqueSet()
and report its count as 1.
add
in interface Collection<E>
add
in interface MultiSet<E>
add
in class AbstractCollection<E>
object
- the object to addtrue
always, as the size of the MultiSet is increased
in any casepublic int add(E object, int occurrences)
MultiSet
If the object is already in the MultiSet.uniqueSet()
then increment its
count as reported by MultiSet.getCount(Object)
. Otherwise add it to the
MultiSet.uniqueSet()
and report its count as occurrences
.
public void clear()
clear
in interface Collection<E>
clear
in class AbstractCollection<E>
public boolean remove(Object object)
MultiSet
If the number of occurrences after this operations is reduced
to zero, the object will be removed from the MultiSet.uniqueSet()
.
remove
in interface Collection<E>
remove
in interface MultiSet<E>
remove
in class AbstractCollection<E>
object
- the object to removetrue
if this call changed the collectionpublic int remove(Object object, int occurrences)
MultiSet
If the number of occurrences to remove is greater than the actual number of occurrences in the multiset, the object will be removed from the multiset.
remove
in interface MultiSet<E>
object
- the object to removeoccurrences
- the number of occurrences to remove, may be zero,
in which case no change is made to the multisetpublic boolean removeAll(Collection<?> coll)
MultiSet
removeAll
in interface Collection<E>
removeAll
in interface MultiSet<E>
removeAll
in class AbstractCollection<E>
coll
- the collection of elements to removetrue
if this call changed the multisetprotected Set<E> createUniqueSet()
protected Iterator<E> createUniqueSetIterator()
public Set<MultiSet.Entry<E>> entrySet()
protected Set<MultiSet.Entry<E>> createEntrySet()
protected abstract int uniqueElements()
protected abstract Iterator<MultiSet.Entry<E>> createEntrySetIterator()
protected void doWriteObject(ObjectOutputStream out) throws IOException
out
- the output streamIOException
- any of the usual I/O related exceptionsprotected void doReadObject(ObjectInputStream in) throws IOException, ClassNotFoundException
in
- the input streamIOException
- any of the usual I/O related exceptionsClassNotFoundException
- if the stream contains an object which class can not be loadedClassCastException
- if the stream does not contain the correct objectspublic boolean equals(Object object)
MultiSet
This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.
public int hashCode()
MultiSet
(e==null ? 0 : e.hashCode()) ^ noOccurances)
.public String toString()
toString
in class AbstractCollection<E>
Copyright © 2001–2019 The Apache Software Foundation. All rights reserved.