Class TreeMultiset<E>
java.lang.Object
java.util.AbstractCollection<E>
be.imgn.gaia.core.collect.TreeMultiset<E>
- Type Parameters:
E- the element type
- All Implemented Interfaces:
Multiset<E>, SortedMultiset<E>, Serializable, Iterable<E>, Collection<E>, SequencedCollection<E>
public final class TreeMultiset<E>
extends AbstractCollection<E>
implements SortedMultiset<E>, Serializable
A modifiable sorted multiset backed by a
TreeMap, the multiset counterpart of TreeSet. Its
ordering must be consistent with equals: two elements are the same element when they compare equal.
- It iterates in ascending order, with all occurrences of an element in a row.
- It rejects
nullelements. Querying it withnull, as incount(null), throwsNullPointerExceptionunless its comparator acceptsnull. - Its views and entries are live, and its iterators support
Iterator.remove(). - Its iterators fail fast when an element is added or removed while they iterate, outside of their own
remove(). A change of the count of an element already there does not disturb them. - Finding, adding and removing an element take a time logarithmic in the number of distinct elements.
Collection.size()takes a constant time on the multiset and its reversed view, and a time linear in the number of distinct elements on a range view. - It is serializable if all its elements and its comparator are. A view is serialized as a copy.
- It is not thread-safe.
- Since:
- 0.1.1
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface Multiset
Multiset.Entry<E> -
Method Summary
Modifier and TypeMethodDescriptionbooleanintAdds occurrences of an element.booleanaddAll(Collection<? extends E> c) ceilingEntry(E element) Returns the entry of the lowest element higher than or equal to an element.voidclear()@Nullable Comparator<? super E> Returns the comparator that orders the elements of this multiset.booleanintReturns the number of occurrences of an element.static <E extends Comparable<? super E>>
TreeMultiset<E> create()Returns a new, empty multiset sorted by the natural ordering of its elements.static <E extends Comparable<? super E>>
TreeMultiset<E> create(Collection<? extends E> elements) Returns a new multiset sorted by the natural ordering of its elements, containing the elements of a collection, each as many times as it occurs there.static <E> TreeMultiset<E> create(Comparator<? super E> comparator) Returns a new, empty multiset sorted by a comparator.Returns a view of the distinct elements of this multiset.entrySet()Returns a view of the distinct elements of this multiset with their counts, one entry per element, in the order ofMultiset.elementSet().booleanCompares this multiset with an object.Returns the entry of the lowest element.floorEntry(E element) Returns the entry of the highest element lower than or equal to an element.voidforEachEntry(ObjIntConsumer<? super E> action) Calls an action with each distinct element of this multiset and its count, in the order ofMultiset.elementSet().inthashCode()Returns the hash code of this multiset: the hash code of itsMultiset.entrySet(), the sum of the hash codes of its entries.headMultiset(E toElement, boolean inclusive) Returns a view of the elements lower than an element, or equal to it ifinclusiveistrue.higherEntry(E element) Returns the entry of the lowest element strictly higher than an element.booleanisEmpty()iterator()Returns the entry of the highest element.lowerEntry(E element) Returns the entry of the highest element strictly lower than an element.Removes all occurrences of the lowest element, and returns its entry as it was.Removes all occurrences of the highest element, and returns its entry as it was.booleanintRemoves occurrences of an element, as many as there are if there are fewer.booleanremoveAll(Collection<?> c) booleanbooleanretainAll(Collection<?> c) reversed()Returns a view of this multiset in descending order.intSets the number of occurrences of an element, adding or removing occurrences as needed.intsize()subMultiset(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) Returns a view of the elements between two elements.tailMultiset(E fromElement, boolean inclusive) Returns a view of the elements higher than an element, or equal to it ifinclusiveistrue.toString()Returns each element once, followed by×and its count if it occurs more than once, as in[foo × 3, bar]: the occurrences may be too many to write one by one.Methods inherited from class AbstractCollection
containsAll, toArray, toArrayMethods inherited from interface Collection
add, addAll, clear, contains, containsAll, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArrayMethods inherited from interface Multiset
add, count, entrySet, equals, forEachEntry, hashCode, remove, setCountMethods inherited from interface SequencedCollection
addFirst, addLast, getFirst, getLast, removeFirst, removeLast
-
Method Details
-
create
Returns a new, empty multiset sorted by the natural ordering of its elements.- Type Parameters:
E- the element type- Returns:
- a new, empty multiset
-
create
public static <E extends Comparable<? super E>> TreeMultiset<E> create(Collection<? extends E> elements) Returns a new multiset sorted by the natural ordering of its elements, containing the elements of a collection, each as many times as it occurs there.- Type Parameters:
E- the element type- Parameters:
elements- the elements to add- Returns:
- a new multiset containing
elements
-
create
Returns a new, empty multiset sorted by a comparator.- Type Parameters:
E- the element type- Parameters:
comparator- the comparator that orders the elements- Returns:
- a new, empty multiset
-
comparator
Description copied from interface:SortedMultisetReturns the comparator that orders the elements of this multiset.- Specified by:
comparatorin interfaceSortedMultiset<E>- Returns:
- the comparator, or
nullif the elements are in their natural ordering
-
elementSet
Description copied from interface:MultisetReturns a view of the distinct elements of this multiset. Removing an element from the view removes all its occurrences from this multiset. The view does not support adding elements.- Specified by:
elementSetin interfaceMultiset<E>- Specified by:
elementSetin interfaceSortedMultiset<E>- Returns:
- the distinct elements of this multiset
-
firstEntry
Description copied from interface:SortedMultisetReturns the entry of the lowest element.- Specified by:
firstEntryin interfaceSortedMultiset<E>- Returns:
- the entry of the lowest element, or an empty
Optionalif this multiset is empty
-
lastEntry
Description copied from interface:SortedMultisetReturns the entry of the highest element.- Specified by:
lastEntryin interfaceSortedMultiset<E>- Returns:
- the entry of the highest element, or an empty
Optionalif this multiset is empty
-
lowerEntry
Description copied from interface:SortedMultisetReturns the entry of the highest element strictly lower than an element.- Specified by:
lowerEntryin interfaceSortedMultiset<E>- Parameters:
element- the element to compare with- Returns:
- the entry of the highest element lower than
element, or an emptyOptionalif there is none
-
floorEntry
Description copied from interface:SortedMultisetReturns the entry of the highest element lower than or equal to an element.- Specified by:
floorEntryin interfaceSortedMultiset<E>- Parameters:
element- the element to compare with- Returns:
- the entry of the highest element lower than or equal to
element, or an emptyOptionalif there is none
-
ceilingEntry
Description copied from interface:SortedMultisetReturns the entry of the lowest element higher than or equal to an element.- Specified by:
ceilingEntryin interfaceSortedMultiset<E>- Parameters:
element- the element to compare with- Returns:
- the entry of the lowest element higher than or equal to
element, or an emptyOptionalif there is none
-
higherEntry
Description copied from interface:SortedMultisetReturns the entry of the lowest element strictly higher than an element.- Specified by:
higherEntryin interfaceSortedMultiset<E>- Parameters:
element- the element to compare with- Returns:
- the entry of the lowest element higher than
element, or an emptyOptionalif there is none
-
pollFirstEntry
Description copied from interface:SortedMultisetRemoves all occurrences of the lowest element, and returns its entry as it was.- Specified by:
pollFirstEntryin interfaceSortedMultiset<E>- Returns:
- the entry of the element removed, which no longer changes, or an empty
Optionalif this multiset is empty
-
pollLastEntry
Description copied from interface:SortedMultisetRemoves all occurrences of the highest element, and returns its entry as it was.- Specified by:
pollLastEntryin interfaceSortedMultiset<E>- Returns:
- the entry of the element removed, which no longer changes, or an empty
Optionalif this multiset is empty
-
headMultiset
Description copied from interface:SortedMultisetReturns a view of the elements lower than an element, or equal to it ifinclusiveistrue.- Specified by:
headMultisetin interfaceSortedMultiset<E>- Parameters:
toElement- the upper bound of the viewinclusive- whether the view includestoElement- Returns:
- a view of the elements up to
toElement
-
tailMultiset
Description copied from interface:SortedMultisetReturns a view of the elements higher than an element, or equal to it ifinclusiveistrue.- Specified by:
tailMultisetin interfaceSortedMultiset<E>- Parameters:
fromElement- the lower bound of the viewinclusive- whether the view includesfromElement- Returns:
- a view of the elements from
fromElement
-
subMultiset
public SortedMultiset<E> subMultiset(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) Description copied from interface:SortedMultisetReturns a view of the elements between two elements.- Specified by:
subMultisetin interfaceSortedMultiset<E>- Parameters:
fromElement- the lower bound of the viewfromInclusive- whether the view includesfromElementtoElement- the upper bound of the viewtoInclusive- whether the view includestoElement- Returns:
- a view of the elements from
fromElementtotoElement
-
reversed
Description copied from interface:SortedMultisetReturns a view of this multiset in descending order. Its comparator is the reverse of the comparator of this multiset.- Specified by:
reversedin interfaceSequencedCollection<E>- Specified by:
reversedin interfaceSortedMultiset<E>- Returns:
- a view of this multiset in descending order
-
size
public int size()- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>
-
count
-
iterator
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>
-
spliterator
- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>
-
add
- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractCollection<E>
-
add
Description copied from interface:MultisetAdds occurrences of an element. -
remove
- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>
-
remove
Description copied from interface:MultisetRemoves occurrences of an element, as many as there are if there are fewer. -
setCount
Description copied from interface:MultisetSets the number of occurrences of an element, adding or removing occurrences as needed. -
addAll
- Specified by:
addAllin interfaceCollection<E>- Overrides:
addAllin classAbstractCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>- Overrides:
removeAllin classAbstractCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>- Overrides:
retainAllin classAbstractCollection<E>
-
removeIf
- Specified by:
removeIfin interfaceCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
entrySet
Description copied from interface:MultisetReturns a view of the distinct elements of this multiset with their counts, one entry per element, in the order ofMultiset.elementSet(). Removing an entry from the view removes all occurrences of its element from this multiset. The view does not support adding entries. -
forEachEntry
Description copied from interface:MultisetCalls an action with each distinct element of this multiset and its count, in the order ofMultiset.elementSet().- Specified by:
forEachEntryin interfaceMultiset<E>- Parameters:
action- the action to call
-
equals
Description copied from interface:MultisetCompares this multiset with an object. They are equal if the object is also aMultisetand both contain the same elements with the same counts, whatever their order or implementation. -
hashCode
public int hashCode()Description copied from interface:MultisetReturns the hash code of this multiset: the hash code of itsMultiset.entrySet(), the sum of the hash codes of its entries. Equal multisets have equal hash codes, whatever their implementation. -
toString
Returns each element once, followed by×and its count if it occurs more than once, as in[foo × 3, bar]: the occurrences may be too many to write one by one.- Overrides:
toStringin classAbstractCollection<E>
-