Class TreeMultiset<E>

All Implemented Interfaces:
Multiset<E>, SortedIterable<E>, SortedMultiset<E>, SortedMultisetBridge<E>, Serializable, Iterable<E>, Collection<E>

public final class TreeMultiset<E> extends AbstractSortedMultiset<E> implements Serializable
A multiset which maintains the ordering of its elements, according to either their natural order or an explicit Comparator. In all cases, this implementation uses Comparable.compareTo(T) or Comparator.compare(T, T) instead of Object.equals(java.lang.Object) to determine equivalence of instances.

Warning: The comparison must be consistent with equals as explained by the Comparable class specification. Otherwise, the resulting multiset will violate the Collection contract, which is specified in terms of Object.equals(java.lang.Object).

See the Guava User Guide article on Multiset.

Since:
2.0
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • create

      public static <E extends Comparable> TreeMultiset<E> create()
      Creates a new, empty multiset, sorted according to the elements' natural order. All elements inserted into the multiset must implement the Comparable interface. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the multiset. If the user attempts to add an element to the multiset that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add(Object) call will throw a ClassCastException.

      The type specification is <E extends Comparable>, instead of the more specific <E extends Comparable<? super E>>, to support classes defined without generics.

    • create

      public static <E> TreeMultiset<E> create(@CheckForNull Comparator<? super E> comparator)
      Creates a new, empty multiset, sorted according to the specified comparator. All elements inserted into the multiset must be mutually comparable by the specified comparator: comparator.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the multiset. If the user attempts to add an element to the multiset that violates this constraint, the add(Object) call will throw a ClassCastException.
      Parameters:
      comparator - the comparator that will be used to sort this multiset. A null value indicates that the elements' natural ordering should be used.
    • create

      public static <E extends Comparable> TreeMultiset<E> create(Iterable<? extends E> elements)
      Creates an empty multiset containing the given initial elements, sorted according to the elements' natural order.

      This implementation is highly efficient when elements is itself a Multiset.

      The type specification is <E extends Comparable>, instead of the more specific <E extends Comparable<? super E>>, to support classes defined without generics.

    • aggregateForEntries

      private long aggregateForEntries(TreeMultiset.Aggregate aggr)
    • aggregateBelowRange

      private long aggregateBelowRange(TreeMultiset.Aggregate aggr, @CheckForNull TreeMultiset.AvlNode<E> node)
    • aggregateAboveRange

      private long aggregateAboveRange(TreeMultiset.Aggregate aggr, @CheckForNull TreeMultiset.AvlNode<E> node)
    • size

      public int size()
      Description copied from interface: Multiset
      Returns the total number of all occurrences of all elements in this multiset.

      Note: this method does not return the number of distinct elements in the multiset, which is given by entrySet().size().

      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Multiset<E>
      Specified by:
      size in class AbstractCollection<E>
    • distinctElements

      int distinctElements()
      Specified by:
      distinctElements in class AbstractMultiset<E>
    • distinctElements

      static int distinctElements(@CheckForNull TreeMultiset.AvlNode<?> node)
    • count

      public int count(@CheckForNull Object element)
      Description copied from interface: Multiset
      Returns the number of occurrences of an element in this multiset (the count of the element). Note that for an Object.equals(java.lang.Object)-based multiset, this gives the same result as Collections.frequency(java.util.Collection<?>, java.lang.Object) (which would presumably perform more poorly).

      Note: the utility method Iterables.frequency(java.lang.Iterable<?>, java.lang.Object) generalizes this operation; it correctly delegates to this method when dealing with a multiset, but it can also accept any other iterable type.

      Specified by:
      count in interface Multiset<E>
      Parameters:
      element - the element to count occurrences of
      Returns:
      the number of occurrences of the element in this multiset; possibly zero but never negative
    • add

      public int add(E element, int occurrences)
      Description copied from interface: Multiset
      Adds a number of occurrences of an element to this multiset. Note that if occurrences == 1, this method has the identical effect to Multiset.add(Object). This method is functionally equivalent (except in the case of overflow) to the call addAll(Collections.nCopies(element, occurrences)), which would presumably perform much more poorly.
      Specified by:
      add in interface Multiset<E>
      Overrides:
      add in class AbstractMultiset<E>
      Parameters:
      element - the element to add occurrences of; may be null only if explicitly allowed by the implementation
      occurrences - the number of occurrences of the element to add. May be zero, in which case no change will be made.
      Returns:
      the count of the element before the operation; possibly zero
    • remove

      public int remove(@CheckForNull Object element, int occurrences)
      Description copied from interface: Multiset
      Removes a number of occurrences of the specified element from this multiset. If the multiset contains fewer than this number of occurrences to begin with, all occurrences will be removed. Note that if occurrences == 1, this is functionally equivalent to the call remove(element).
      Specified by:
      remove in interface Multiset<E>
      Overrides:
      remove in class AbstractMultiset<E>
      Parameters:
      element - the element to conditionally remove occurrences of
      occurrences - the number of occurrences of the element to remove. May be zero, in which case no change will be made.
      Returns:
      the count of the element before the operation; possibly zero
    • setCount

      public int setCount(E element, int count)
      Description copied from interface: Multiset
      Adds or removes the necessary occurrences of an element such that the element attains the desired count.
      Specified by:
      setCount in interface Multiset<E>
      Overrides:
      setCount in class AbstractMultiset<E>
      Parameters:
      element - the element to add or remove occurrences of; may be null only if explicitly allowed by the implementation
      count - the desired count of the element in this multiset
      Returns:
      the count of the element before the operation; possibly zero
    • setCount

      public boolean setCount(E element, int oldCount, int newCount)
      Description copied from interface: Multiset
      Conditionally sets the count of an element to a new value, as described in Multiset.setCount(Object, int), provided that the element has the expected current count. If the current count is not oldCount, no change is made.
      Specified by:
      setCount in interface Multiset<E>
      Overrides:
      setCount in class AbstractMultiset<E>
      Parameters:
      element - the element to conditionally set the count of; may be null only if explicitly allowed by the implementation
      oldCount - the expected present count of the element in this multiset
      newCount - the desired count of the element in this multiset
      Returns:
      true if the condition for modification was met. This implies that the multiset was indeed modified, unless oldCount == newCount.
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in class AbstractMultiset<E>
    • wrapEntry

      private Multiset.Entry<E> wrapEntry(TreeMultiset.AvlNode<E> baseEntry)
    • firstNode

      @CheckForNull private TreeMultiset.AvlNode<E> firstNode()
      Returns the first node in the tree that is in range.
    • lastNode

      @CheckForNull private TreeMultiset.AvlNode<E> lastNode()
    • elementIterator

      Iterator<E> elementIterator()
      Specified by:
      elementIterator in class AbstractMultiset<E>
    • entryIterator

      Iterator<Multiset.Entry<E>> entryIterator()
      Specified by:
      entryIterator in class AbstractMultiset<E>
    • descendingEntryIterator

      Iterator<Multiset.Entry<E>> descendingEntryIterator()
      Specified by:
      descendingEntryIterator in class AbstractSortedMultiset<E>
    • forEachEntry

      public void forEachEntry(ObjIntConsumer<? super E> action)
      Description copied from interface: Multiset
      Runs the specified action for each distinct element in this multiset, and the number of occurrences of that element. For some Multiset implementations, this may be more efficient than iterating over the Multiset.entrySet() either explicitly or with entrySet().forEach(action).
      Specified by:
      forEachEntry in interface Multiset<E>
    • iterator

      public Iterator<E> iterator()
      Description copied from interface: SortedMultiset

      Elements that occur multiple times in the multiset will appear multiple times in this iterator, though not necessarily sequentially.

      The iterator returns the elements in ascending order according to this multiset's comparator.

      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Multiset<E>
      Specified by:
      iterator in interface SortedIterable<E>
      Specified by:
      iterator in interface SortedMultiset<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • headMultiset

      public SortedMultiset<E> headMultiset(E upperBound, BoundType boundType)
      Description copied from interface: SortedMultiset
      Returns a view of this multiset restricted to the elements less than upperBound, optionally including upperBound itself. The returned multiset is a view of this multiset, so changes to one will be reflected in the other. The returned multiset supports all operations that this multiset supports.

      The returned multiset will throw an IllegalArgumentException on attempts to add elements outside its range.

      Specified by:
      headMultiset in interface SortedMultiset<E>
    • tailMultiset

      public SortedMultiset<E> tailMultiset(E lowerBound, BoundType boundType)
      Description copied from interface: SortedMultiset
      Returns a view of this multiset restricted to the elements greater than lowerBound, optionally including lowerBound itself. The returned multiset is a view of this multiset, so changes to one will be reflected in the other. The returned multiset supports all operations that this multiset supports.

      The returned multiset will throw an IllegalArgumentException on attempts to add elements outside its range.

      Specified by:
      tailMultiset in interface SortedMultiset<E>
    • successor

      private static <T> void successor(TreeMultiset.AvlNode<T> a, TreeMultiset.AvlNode<T> b)
    • successor

      private static <T> void successor(TreeMultiset.AvlNode<T> a, TreeMultiset.AvlNode<T> b, TreeMultiset.AvlNode<T> c)
    • writeObject

      private void writeObject(ObjectOutputStream stream) throws IOException
      Throws:
      IOException
    • readObject

      private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
      Throws:
      IOException
      ClassNotFoundException