Class AbstractMultiSet<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- org.apache.commons.collections4.multiset.AbstractMultiSet<E>
-
- Type Parameters:
E
- the type held in the multiset
- All Implemented Interfaces:
java.lang.Iterable<E>
,java.util.Collection<E>
,MultiSet<E>
- Direct Known Subclasses:
AbstractMapMultiSet
public abstract class AbstractMultiSet<E> extends java.util.AbstractCollection<E> implements MultiSet<E>
Abstract implementation of theMultiSet
interface to simplify the creation of subclass implementations.- Since:
- 4.1
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.commons.collections4.MultiSet
MultiSet.Entry<E>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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(java.lang.Object object)
Determines if the multiset contains the given element.java.util.Set<MultiSet.Entry<E>>
entrySet()
Returns an unmodifiable view of the entries of this multiset.boolean
equals(java.lang.Object object)
Compares this MultiSet to another object.int
getCount(java.lang.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.java.util.Iterator<E>
iterator()
Gets an iterator over the multiset elements.boolean
remove(java.lang.Object object)
Removes one occurrence of the given object from the MultiSet.int
remove(java.lang.Object object, int occurrences)
Removes a number of occurrences of the specified object from the MultiSet.boolean
removeAll(java.util.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.java.lang.String
toString()
Implement a toString() method suitable for debugging.java.util.Set<E>
uniqueSet()
Returns a view of the unique elements of this multiset.-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, isEmpty, retainAll, toArray, toArray
-
Methods inherited from interface java.util.Collection
addAll, isEmpty, parallelStream, removeIf, spliterator, stream, toArray, toArray, toArray
-
Methods inherited from interface org.apache.commons.collections4.MultiSet
containsAll, retainAll
-
-
-
-
Method Detail
-
size
public int size()
Returns the number of elements in this multiset.
-
getCount
public int getCount(java.lang.Object object)
Returns the number of occurrence of the given element in this multiset by iterating over its entrySet.
-
setCount
public int setCount(E object, int count)
Description copied from interface:MultiSet
Sets the number of occurrences of the specified object in the MultiSet to the given count.If the provided count is zero, the object will be removed from the
MultiSet.uniqueSet()
.
-
contains
public boolean contains(java.lang.Object object)
Determines if the multiset contains the given element.
-
iterator
public java.util.Iterator<E> iterator()
Gets an iterator over the multiset elements. Elements present in the MultiSet more than once will be returned repeatedly.
-
add
public boolean add(E object)
Description copied from interface:MultiSet
Adds one copy of the specified object to the MultiSet.If the object is already in the
MultiSet.uniqueSet()
then increment its count as reported byMultiSet.getCount(Object)
. Otherwise add it to theMultiSet.uniqueSet()
and report its count as 1.
-
add
public int add(E object, int occurrences)
Description copied from interface:MultiSet
Adds a number of occurrences of the specified object to the MultiSet.If the object is already in the
MultiSet.uniqueSet()
then increment its count as reported byMultiSet.getCount(Object)
. Otherwise add it to theMultiSet.uniqueSet()
and report its count asoccurrences
.
-
clear
public void clear()
Clears the multiset removing all elements from the entrySet.
-
remove
public boolean remove(java.lang.Object object)
Description copied from interface:MultiSet
Removes one occurrence of the given object from the MultiSet.If the number of occurrences after this operations is reduced to zero, the object will be removed from the
MultiSet.uniqueSet()
.
-
remove
public int remove(java.lang.Object object, int occurrences)
Description copied from interface:MultiSet
Removes a number of occurrences of the specified object from the 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.
- Specified by:
remove
in interfaceMultiSet<E>
- Parameters:
object
- the object to removeoccurrences
- the number of occurrences to remove, may be zero, in which case no change is made to the multiset- Returns:
- the number of occurrences of the object in the multiset before the operation; possibly zero
-
removeAll
public boolean removeAll(java.util.Collection<?> coll)
Description copied from interface:MultiSet
Remove all occurrences of all elements from this MultiSet represented in the given collection.
-
uniqueSet
public java.util.Set<E> uniqueSet()
Returns a view of the unique elements of this multiset.
-
entrySet
public java.util.Set<MultiSet.Entry<E>> entrySet()
Returns an unmodifiable view of the entries of this multiset.
-
equals
public boolean equals(java.lang.Object object)
Description copied from interface:MultiSet
Compares this MultiSet to another object.This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.
-
hashCode
public int hashCode()
Description copied from interface:MultiSet
Gets a hash code for the MultiSet compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as(e==null ? 0 : e.hashCode()) ^ noOccurances)
.
-
toString
public java.lang.String toString()
Implement a toString() method suitable for debugging.- Overrides:
toString
in classjava.util.AbstractCollection<E>
- Returns:
- a debugging toString
-
-