SortedMultiset
Date: 2016-05-24

com.google.common.collect

## Interface SortedMultiset<E>

- All Known Implementing Classes:
    ForwardingSortedMultiset, ForwardingSortedMultiset.StandardDescendingMultiset, ImmutableSortedMultiset, TreeMultiset

      @Beta
      @GwtCompatible(emulated=true)
      public interface SortedMultiset<E>

  A `Multiset` which maintains the ordering of its elements, according to either their natural order or an explicit `Comparator`. This order is reflected when iterating over the sorted multiset, either directly, or through its `elementSet` or `entrySet` views. 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 it is specified in terms of `Object.equals(java.lang.Object)`.

  See the Guava User Guide article on `Multiset`.

  Since:
    11.0

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface com.google.common.collect.Multiset

      `Multiset.Entry<E>`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.util.Comparator<? super E>` | `comparator()` Returns the comparator that orders this multiset, or `Ordering.natural()` if the natural ordering of the elements is used.
    `SortedMultiset<E>` | `descendingMultiset()` Returns a descending view of this multiset.
    `java.util.NavigableSet<E>` | `elementSet()` Returns a `NavigableSet` view of the distinct elements in this multiset.
    `java.util.Set<Multiset.Entry<E>>` | `entrySet()` Returns a view of the contents of this multiset, grouped into `Multiset.Entry` instances, each providing an element of the multiset and the count of that element.
    `Multiset.Entry<E>` | `firstEntry()` Returns the entry of the first element in this multiset, or `null` if this multiset is empty.
    `SortedMultiset<E>` | `headMultiset(E upperBound, BoundType boundType)` Returns a view of this multiset restricted to the elements less than `upperBound`, optionally including `upperBound` itself.
    `java.util.Iterator<E>` | `iterator()`
    `Multiset.Entry<E>` | `lastEntry()` Returns the entry of the last element in this multiset, or `null` if this multiset is empty.
    `Multiset.Entry<E>` | `pollFirstEntry()` Returns and removes the entry associated with the lowest element in this multiset, or returns `null` if this multiset is empty.
    `Multiset.Entry<E>` | `pollLastEntry()` Returns and removes the entry associated with the greatest element in this multiset, or returns `null` if this multiset is empty.
    `SortedMultiset<E>` | `subMultiset(E lowerBound, BoundType lowerBoundType, E upperBound, BoundType upperBoundType)` Returns a view of this multiset restricted to the range between `lowerBound` and `upperBound`.
    `SortedMultiset<E>` | `tailMultiset(E lowerBound, BoundType boundType)` Returns a view of this multiset restricted to the elements greater than `lowerBound`, optionally including `lowerBound` itself.

    - ### Methods inherited from interface com.google.common.collect.Multiset

      `add, add, contains, containsAll, count, equals, hashCode, remove, remove, removeAll, retainAll, setCount, setCount, toString`

    - ### Methods inherited from interface java.util.Collection

      `addAll, clear, isEmpty, size, toArray, toArray`

- - ### Method Detail

    - #### comparator

          java.util.Comparator<? super E> comparator()

      Returns the comparator that orders this multiset, or `Ordering.natural()` if the natural ordering of the elements is used.

    - #### firstEntry

          Multiset.Entry<E> firstEntry()

      Returns the entry of the first element in this multiset, or `null` if this multiset is empty.

    - #### lastEntry

          Multiset.Entry<E> lastEntry()

      Returns the entry of the last element in this multiset, or `null` if this multiset is empty.

    - #### pollFirstEntry

          Multiset.Entry<E> pollFirstEntry()

      Returns and removes the entry associated with the lowest element in this multiset, or returns `null` if this multiset is empty.

    - #### pollLastEntry

          Multiset.Entry<E> pollLastEntry()

      Returns and removes the entry associated with the greatest element in this multiset, or returns `null` if this multiset is empty.

    - #### elementSet

          java.util.NavigableSet<E> elementSet()

      Returns a `NavigableSet` view of the distinct elements in this multiset.
      Returns:
        a view of the set of distinct elements in this multiset
      Since:
        14.0 (present with return type `SortedSet` since 11.0)

    - #### entrySet

          java.util.Set<Multiset.Entry<E>> entrySet()

      Returns a view of the contents of this multiset, grouped into `Multiset.Entry` instances, each providing an element of the multiset and the count of that element. This set contains exactly one entry for each distinct element in the multiset (thus it always has the same size as the `Multiset.elementSet()`). The order of the elements in the element set is unspecified.

      The entry set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. However, multiset changes may or may not be reflected in any `Entry` instances already retrieved from the entry set (this is implementation-dependent). Furthermore, implementations are not required to support modifications to the entry set at all, and the `Entry` instances themselves don't even have methods for modification. See the specific implementation class for more details on how its entry set handles modifications.

      The `entrySet`'s iterator returns entries in ascending element order according to the this multiset's comparator.

      Returns:
        a set of entries representing the data of this multiset

    - #### iterator

          java.util.Iterator<E> iterator()

      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.

    - #### descendingMultiset

          SortedMultiset<E> descendingMultiset()

      Returns a descending view of this multiset. Modifications made to either map will be reflected in the other.

    - #### headMultiset

          SortedMultiset<E> headMultiset(E upperBound,
                                       BoundType boundType)

      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.

    - #### subMultiset

          SortedMultiset<E> subMultiset(E lowerBound,
                                      BoundType lowerBoundType,
                                      E upperBound,
                                      BoundType upperBoundType)

      Returns a view of this multiset restricted to the range between `lowerBound` and `upperBound`. 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.

      This method is equivalent to `tailMultiset(lowerBound, lowerBoundType).headMultiset(upperBound, upperBoundType)`.

    - #### tailMultiset

          SortedMultiset<E> tailMultiset(E lowerBound,
                                       BoundType boundType)

      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.