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 null elements. Querying it with null, as in count(null), throws NullPointerException unless its comparator accepts null.
  • 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:
  • Method Details

    • create

      public static <E extends Comparable<? super E>> TreeMultiset<E> 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

      public static <E> TreeMultiset<E> create(Comparator<? super E> comparator)
      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

      public @Nullable Comparator<? super E> comparator()
      Description copied from interface: SortedMultiset
      Returns the comparator that orders the elements of this multiset.
      Specified by:
      comparator in interface SortedMultiset<E>
      Returns:
      the comparator, or null if the elements are in their natural ordering
    • elementSet

      public NavigableSet<E> elementSet()
      Description copied from interface: Multiset
      Returns 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:
      elementSet in interface Multiset<E>
      Specified by:
      elementSet in interface SortedMultiset<E>
      Returns:
      the distinct elements of this multiset
    • firstEntry

      public Optional<Multiset.Entry<E>> firstEntry()
      Description copied from interface: SortedMultiset
      Returns the entry of the lowest element.
      Specified by:
      firstEntry in interface SortedMultiset<E>
      Returns:
      the entry of the lowest element, or an empty Optional if this multiset is empty
    • lastEntry

      public Optional<Multiset.Entry<E>> lastEntry()
      Description copied from interface: SortedMultiset
      Returns the entry of the highest element.
      Specified by:
      lastEntry in interface SortedMultiset<E>
      Returns:
      the entry of the highest element, or an empty Optional if this multiset is empty
    • lowerEntry

      public Optional<Multiset.Entry<E>> lowerEntry(E element)
      Description copied from interface: SortedMultiset
      Returns the entry of the highest element strictly lower than an element.
      Specified by:
      lowerEntry in interface SortedMultiset<E>
      Parameters:
      element - the element to compare with
      Returns:
      the entry of the highest element lower than element, or an empty Optional if there is none
    • floorEntry

      public Optional<Multiset.Entry<E>> floorEntry(E element)
      Description copied from interface: SortedMultiset
      Returns the entry of the highest element lower than or equal to an element.
      Specified by:
      floorEntry in interface SortedMultiset<E>
      Parameters:
      element - the element to compare with
      Returns:
      the entry of the highest element lower than or equal to element, or an empty Optional if there is none
    • ceilingEntry

      public Optional<Multiset.Entry<E>> ceilingEntry(E element)
      Description copied from interface: SortedMultiset
      Returns the entry of the lowest element higher than or equal to an element.
      Specified by:
      ceilingEntry in interface SortedMultiset<E>
      Parameters:
      element - the element to compare with
      Returns:
      the entry of the lowest element higher than or equal to element, or an empty Optional if there is none
    • higherEntry

      public Optional<Multiset.Entry<E>> higherEntry(E element)
      Description copied from interface: SortedMultiset
      Returns the entry of the lowest element strictly higher than an element.
      Specified by:
      higherEntry in interface SortedMultiset<E>
      Parameters:
      element - the element to compare with
      Returns:
      the entry of the lowest element higher than element, or an empty Optional if there is none
    • pollFirstEntry

      public Optional<Multiset.Entry<E>> pollFirstEntry()
      Description copied from interface: SortedMultiset
      Removes all occurrences of the lowest element, and returns its entry as it was.
      Specified by:
      pollFirstEntry in interface SortedMultiset<E>
      Returns:
      the entry of the element removed, which no longer changes, or an empty Optional if this multiset is empty
    • pollLastEntry

      public Optional<Multiset.Entry<E>> pollLastEntry()
      Description copied from interface: SortedMultiset
      Removes all occurrences of the highest element, and returns its entry as it was.
      Specified by:
      pollLastEntry in interface SortedMultiset<E>
      Returns:
      the entry of the element removed, which no longer changes, or an empty Optional if this multiset is empty
    • headMultiset

      public SortedMultiset<E> headMultiset(E toElement, boolean inclusive)
      Description copied from interface: SortedMultiset
      Returns a view of the elements lower than an element, or equal to it if inclusive is true.
      Specified by:
      headMultiset in interface SortedMultiset<E>
      Parameters:
      toElement - the upper bound of the view
      inclusive - whether the view includes toElement
      Returns:
      a view of the elements up to toElement
    • tailMultiset

      public SortedMultiset<E> tailMultiset(E fromElement, boolean inclusive)
      Description copied from interface: SortedMultiset
      Returns a view of the elements higher than an element, or equal to it if inclusive is true.
      Specified by:
      tailMultiset in interface SortedMultiset<E>
      Parameters:
      fromElement - the lower bound of the view
      inclusive - whether the view includes fromElement
      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: SortedMultiset
      Returns a view of the elements between two elements.
      Specified by:
      subMultiset in interface SortedMultiset<E>
      Parameters:
      fromElement - the lower bound of the view
      fromInclusive - whether the view includes fromElement
      toElement - the upper bound of the view
      toInclusive - whether the view includes toElement
      Returns:
      a view of the elements from fromElement to toElement
    • reversed

      public SortedMultiset<E> reversed()
      Description copied from interface: SortedMultiset
      Returns a view of this multiset in descending order. Its comparator is the reverse of the comparator of this multiset.
      Specified by:
      reversed in interface SequencedCollection<E>
      Specified by:
      reversed in interface SortedMultiset<E>
      Returns:
      a view of this multiset in descending order
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in class AbstractCollection<E>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
    • contains

      public boolean contains(@Nullable Object o)
      Specified by:
      contains in interface Collection<E>
      Overrides:
      contains in class AbstractCollection<E>
    • count

      public int count(@Nullable Object element)
      Description copied from interface: Multiset
      Returns the number of occurrences of an element.
      Specified by:
      count in interface Multiset<E>
      Parameters:
      element - the element to count
      Returns:
      the number of occurrences of element, or 0 if it is absent
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • spliterator

      public Spliterator<E> spliterator()
      Specified by:
      spliterator in interface Collection<E>
      Specified by:
      spliterator in interface Iterable<E>
    • add

      public boolean add(E element)
      Specified by:
      add in interface Collection<E>
      Overrides:
      add in class AbstractCollection<E>
    • add

      public int add(E element, int occurrences)
      Description copied from interface: Multiset
      Adds occurrences of an element.
      Specified by:
      add in interface Multiset<E>
      Parameters:
      element - the element to add
      occurrences - the number of occurrences to add, possibly 0
      Returns:
      the number of occurrences of element before the call
    • remove

      public boolean remove(@Nullable Object o)
      Specified by:
      remove in interface Collection<E>
      Overrides:
      remove in class AbstractCollection<E>
    • remove

      public int remove(@Nullable Object element, int occurrences)
      Description copied from interface: Multiset
      Removes occurrences of an element, as many as there are if there are fewer.
      Specified by:
      remove in interface Multiset<E>
      Parameters:
      element - the element to remove
      occurrences - the number of occurrences to remove, possibly 0
      Returns:
      the number of occurrences of element before the call
    • setCount

      public int setCount(E element, int count)
      Description copied from interface: Multiset
      Sets the number of occurrences of an element, adding or removing occurrences as needed.
      Specified by:
      setCount in interface Multiset<E>
      Parameters:
      element - the element
      count - the new number of occurrences of element; 0 removes it
      Returns:
      the number of occurrences of element before the call
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E>
      Overrides:
      addAll in class AbstractCollection<E>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<E>
      Overrides:
      removeAll in class AbstractCollection<E>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<E>
      Overrides:
      retainAll in class AbstractCollection<E>
    • removeIf

      public boolean removeIf(Predicate<? super E> filter)
      Specified by:
      removeIf in interface Collection<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Overrides:
      clear in class AbstractCollection<E>
    • entrySet

      public Set<Multiset.Entry<E>> entrySet()
      Description copied from interface: Multiset
      Returns a view of the distinct elements of this multiset with their counts, one entry per element, in the order of Multiset.elementSet(). Removing an entry from the view removes all occurrences of its element from this multiset. The view does not support adding entries.
      Specified by:
      entrySet in interface Multiset<E>
      Returns:
      the entries of this multiset
    • forEachEntry

      public void forEachEntry(ObjIntConsumer<? super E> action)
      Description copied from interface: Multiset
      Calls an action with each distinct element of this multiset and its count, in the order of Multiset.elementSet().
      Specified by:
      forEachEntry in interface Multiset<E>
      Parameters:
      action - the action to call
    • equals

      public boolean equals(@Nullable Object o)
      Description copied from interface: Multiset
      Compares this multiset with an object. They are equal if the object is also a Multiset and both contain the same elements with the same counts, whatever their order or implementation.
      Specified by:
      equals in interface Collection<E>
      Specified by:
      equals in interface Multiset<E>
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with
      Returns:
      true if o is a multiset with the same elements and counts
    • hashCode

      public int hashCode()
      Description copied from interface: Multiset
      Returns the hash code of this multiset: the hash code of its Multiset.entrySet(), the sum of the hash codes of its entries. Equal multisets have equal hash codes, whatever their implementation.
      Specified by:
      hashCode in interface Collection<E>
      Specified by:
      hashCode in interface Multiset<E>
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of this multiset
    • toString

      public String 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:
      toString in class AbstractCollection<E>