Class AbstractMapMultiSet<E>
java.lang.Object
java.util.AbstractCollection<E>
org.apache.commons.collections4.multiset.AbstractMultiSet<E>
org.apache.commons.collections4.multiset.AbstractMapMultiSet<E>
- Type Parameters:
E- the type held in the multiset
- All Implemented Interfaces:
Iterable<E>,Collection<E>,MultiSet<E>
- Direct Known Subclasses:
HashMultiSet
Abstract implementation of the
MultiSet interface to simplify the
creation of subclass implementations.
Subclasses specify a Map implementation to use as the internal storage. The map will be used to map multiset elements to a number; the number represents the number of occurrences of that element in the multiset.
- Since:
- 4.1
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.commons.collections4.MultiSet
MultiSet.Entry<E> -
Method Summary
Modifier and TypeMethodDescriptionintAdds a number of occurrences of the specified object to the MultiSet.voidclear()Clears the multiset by clearing the underlying map.booleanDetermines if the multiset contains the given element by checking if the underlying map contains the element as a key.booleanCompares this MultiSet to another object.intReturns the number of occurrence of the given element in this multiset by looking up its count in the underlying map.inthashCode()Gets a hash code for the MultiSet compatible with the definition of equals.booleanisEmpty()Returns true if the underlying map is empty.iterator()Gets an iterator over the multiset elements.intRemoves a number of occurrences of the specified object from the MultiSet.intsize()Returns the number of elements in this multiset.Object[]toArray()Returns an array of all of this multiset's elements.<T> T[]toArray(T[] array) Returns an array of all of this multiset's elements.Methods inherited from class org.apache.commons.collections4.multiset.AbstractMultiSet
add, entrySet, remove, removeAll, setCount, toString, uniqueSetMethods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAllMethods inherited from interface java.util.Collection
addAll, parallelStream, removeIf, spliterator, stream, toArrayMethods inherited from interface org.apache.commons.collections4.MultiSet
containsAll, retainAll
-
Method Details
-
size
public int size()Returns the number of elements in this multiset.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceMultiSet<E>- Overrides:
sizein classAbstractMultiSet<E>- Returns:
- current size of the multiset
-
isEmpty
public boolean isEmpty()Returns true if the underlying map is empty.- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
- true if multiset is empty
-
getCount
Returns the number of occurrence of the given element in this multiset by looking up its count in the underlying map. -
contains
Determines if the multiset contains the given element by checking if the underlying map contains the element as a key.- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractMultiSet<E>- Parameters:
object- the object to search for- Returns:
- true if the multiset contains the given element
-
iterator
Gets an iterator over the multiset elements. Elements present in the MultiSet more than once will be returned repeatedly. -
add
Description copied from interface:MultiSetAdds 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.- Specified by:
addin interfaceMultiSet<E>- Overrides:
addin classAbstractMultiSet<E>- Parameters:
object- the object to addoccurrences- the number of occurrences to add, 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 this operation; possibly zero
-
clear
public void clear()Clears the multiset by clearing the underlying map.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractMultiSet<E>
-
remove
Description copied from interface:MultiSetRemoves 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:
removein interfaceMultiSet<E>- Overrides:
removein classAbstractMultiSet<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
-
toArray
Returns an array of all of this multiset's elements.- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array of all of this multiset's elements
-
toArray
public <T> T[] toArray(T[] array) Returns an array of all of this multiset's elements. If the input array has more elements than are in the multiset, trailing elements will be set to null.- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Type Parameters:
T- the type of the array elements- Parameters:
array- the array to populate- Returns:
- an array of all of this multiset's elements
- Throws:
ArrayStoreException- if the runtime type of the specified array is not a supertype of the runtime type of the elements in this listNullPointerException- if the specified array is null
-
equals
Description copied from interface:MultiSetCompares 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.
- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceMultiSet<E>- Overrides:
equalsin classAbstractMultiSet<E>- Parameters:
object- the object to compare to- Returns:
- true if equal
-
hashCode
public int hashCode()Description copied from interface:MultiSetGets 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).- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceMultiSet<E>- Overrides:
hashCodein classAbstractMultiSet<E>- Returns:
- the hash code of the MultiSet
-