Class ImmutableSortedMultiset<E>

  • All Implemented Interfaces:
    Multiset<E>, SortedMultiset<E>, java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>

    @GwtIncompatible
    public abstract class ImmutableSortedMultiset<E>
    extends ImmutableMultiset<E>
    implements SortedMultiset<E>
    A SortedMultiset whose contents will never change, with many other important properties detailed at ImmutableCollection.

    Warning: as with any sorted collection, you are strongly advised not to use a Comparator or Comparable type whose comparison behavior is inconsistent with equals. That is, a.compareTo(b) or comparator.compare(a, b) should equal zero if and only if a.equals(b). If this advice is not followed, the resulting collection will not correctly obey its specification.

    See the Guava User Guide article on immutable collections.

    Since:
    12.0
    Author:
    Louis Wasserman
    See Also:
    Serialized Form
    • Method Detail

      • toImmutableSortedMultiset

        public static <E> java.util.stream.Collector<E,​?,​ImmutableSortedMultiset<E>> toImmutableSortedMultiset​(java.util.Comparator<? super E> comparator)
        Returns a Collector that accumulates the input elements into a new ImmutableMultiset. Elements are sorted by the specified comparator.

        Warning: comparator should be consistent with equals as explained in the Comparator documentation.

        Since:
        21.0
      • toImmutableSortedMultiset

        public static <T extends @Nullable java.lang.Object,​E> java.util.stream.Collector<T,​?,​ImmutableSortedMultiset<E>> toImmutableSortedMultiset​(java.util.Comparator<? super E> comparator,
                                                                                                                                                                      java.util.function.Function<? super T,​? extends E> elementFunction,
                                                                                                                                                                      java.util.function.ToIntFunction<? super T> countFunction)
        Returns a Collector that accumulates elements into an ImmutableSortedMultiset whose elements are the result of applying elementFunction to the inputs, with counts equal to the result of applying countFunction to the inputs.

        If the mapped elements contain duplicates (according to comparator), the first occurrence in encounter order appears in the resulting multiset, with count equal to the sum of the outputs of countFunction.applyAsInt(t) for each t mapped to that element.

        Since:
        22.0
      • of

        public static <E> ImmutableSortedMultiset<E> of()
        Returns the empty immutable sorted multiset.

        Performance note: the instance returned is a singleton.

      • of

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> of​(E e1)
        Returns an immutable sorted multiset containing a single element.
      • of

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> of​(E e1,
                                                                                                E e2)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> of​(E e1,
                                                                                                E e2,
                                                                                                E e3)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> of​(E e1,
                                                                                                E e2,
                                                                                                E e3,
                                                                                                E e4)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> of​(E e1,
                                                                                                E e2,
                                                                                                E e3,
                                                                                                E e4,
                                                                                                E e5)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.
        Throws:
        java.lang.NullPointerException - if any element is null
      • of

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> of​(E e1,
                                                                                                E e2,
                                                                                                E e3,
                                                                                                E e4,
                                                                                                E e5,
                                                                                                E e6,
                                                                                                E... remaining)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.
        Throws:
        java.lang.NullPointerException - if any element is null
      • copyOf

        public static <E extends java.lang.Comparable<? super E>> ImmutableSortedMultiset<E> copyOf​(E[] elements)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.
        Throws:
        java.lang.NullPointerException - if any of elements is null
      • copyOf

        public static <E> ImmutableSortedMultiset<E> copyOf​(java.lang.Iterable<? extends E> elements)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering. To create a copy of a SortedMultiset that preserves the comparator, call copyOfSorted(com.google.common.collect.SortedMultiset<E>) instead. This method iterates over elements at most once.

        Note that if s is a Multiset<String>, then ImmutableSortedMultiset.copyOf(s) returns an ImmutableSortedMultiset<String> containing each of the strings in s, while ImmutableSortedMultiset.of(s) returns an ImmutableSortedMultiset<Multiset<String>> containing one element (the given multiset itself).

        Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

        This method is not type-safe, as it may be called on elements that are not mutually comparable.

        Throws:
        java.lang.ClassCastException - if the elements are not mutually comparable
        java.lang.NullPointerException - if any of elements is null
      • copyOf

        public static <E> ImmutableSortedMultiset<E> copyOf​(java.util.Iterator<? extends E> elements)
        Returns an immutable sorted multiset containing the given elements sorted by their natural ordering.

        This method is not type-safe, as it may be called on elements that are not mutually comparable.

        Throws:
        java.lang.ClassCastException - if the elements are not mutually comparable
        java.lang.NullPointerException - if any of elements is null
      • copyOf

        public static <E> ImmutableSortedMultiset<E> copyOf​(java.util.Comparator<? super E> comparator,
                                                            java.util.Iterator<? extends E> elements)
        Returns an immutable sorted multiset containing the given elements sorted by the given Comparator.
        Throws:
        java.lang.NullPointerException - if comparator or any of elements is null
      • copyOf

        public static <E> ImmutableSortedMultiset<E> copyOf​(java.util.Comparator<? super E> comparator,
                                                            java.lang.Iterable<? extends E> elements)
        Returns an immutable sorted multiset containing the given elements sorted by the given Comparator. This method iterates over elements at most once.

        Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

        Throws:
        java.lang.NullPointerException - if comparator or any of elements is null
      • copyOfSorted

        public static <E> ImmutableSortedMultiset<E> copyOfSorted​(SortedMultiset<E> sortedMultiset)
        Returns an immutable sorted multiset containing the elements of a sorted multiset, sorted by the same Comparator. That behavior differs from copyOf(Iterable), which always uses the natural ordering of the elements.

        Despite the method name, this method attempts to avoid actually copying the data when it is safe to do so. The exact circumstances under which a copy will or will not be performed are undocumented and subject to change.

        This method is safe to use even when sortedMultiset is a synchronized or concurrent collection that is currently being modified by another thread.

        Throws:
        java.lang.NullPointerException - if sortedMultiset or any of its elements is null
      • elementSet

        public abstract ImmutableSortedSet<EelementSet()
        Description copied from interface: Multiset
        Returns the set of distinct elements contained in this multiset. The element set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. The order of the elements in the element set is unspecified.

        If the element set supports any removal operations, these necessarily cause all occurrences of the removed element(s) to be removed from the multiset. Implementations are not expected to support the add operations, although this is possible.

        A common use for the element set is to find the number of distinct elements in the multiset: elementSet().size().

        Specified by:
        elementSet in interface Multiset<E>
        Specified by:
        elementSet in interface SortedMultiset<E>
        Specified by:
        elementSet in class ImmutableMultiset<E>
        Returns:
        a view of the set of distinct elements in this multiset
      • pollFirstEntry

        @CanIgnoreReturnValue
        @Deprecated
        @CheckForNull
        public final Multiset.Entry<EpollFirstEntry()
        Deprecated.
        Unsupported operation.
        Returns and removes the entry associated with the lowest element in this multiset, or returns null if this multiset is empty.

        This implementation is guaranteed to throw an UnsupportedOperationException.

        Specified by:
        pollFirstEntry in interface SortedMultiset<E>
        Throws:
        java.lang.UnsupportedOperationException - always
      • pollLastEntry

        @CanIgnoreReturnValue
        @Deprecated
        @CheckForNull
        public final Multiset.Entry<EpollLastEntry()
        Deprecated.
        Unsupported operation.
        Returns and removes the entry associated with the greatest element in this multiset, or returns null if this multiset is empty.

        This implementation is guaranteed to throw an UnsupportedOperationException.

        Specified by:
        pollLastEntry in interface SortedMultiset<E>
        Throws:
        java.lang.UnsupportedOperationException - always
      • headMultiset

        public abstract ImmutableSortedMultiset<EheadMultiset​(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>
      • subMultiset

        public ImmutableSortedMultiset<EsubMultiset​(E lowerBound,
                                                      BoundType lowerBoundType,
                                                      E upperBound,
                                                      BoundType upperBoundType)
        Description copied from interface: SortedMultiset
        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).

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

        public abstract ImmutableSortedMultiset<EtailMultiset​(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>
      • orderedBy

        public static <E> ImmutableSortedMultiset.Builder<E> orderedBy​(java.util.Comparator<E> comparator)
        Returns a builder that creates immutable sorted multisets with an explicit comparator. If the comparator has a more general type than the set being generated, such as creating a SortedMultiset<Integer> with a Comparator<Number>, use the ImmutableSortedMultiset.Builder constructor instead.
        Throws:
        java.lang.NullPointerException - if comparator is null
      • reverseOrder

        public static <E extends java.lang.Comparable<?>> ImmutableSortedMultiset.Builder<E> reverseOrder()
        Returns a builder that creates immutable sorted multisets whose elements are ordered by the reverse of their natural ordering.

        Note: the type parameter E extends Comparable<?> rather than Comparable<? super E> as a workaround for javac bug 6468354.

      • naturalOrder

        public static <E extends java.lang.Comparable<?>> ImmutableSortedMultiset.Builder<E> naturalOrder()
        Returns a builder that creates immutable sorted multisets whose elements are ordered by their natural ordering. The sorted multisets use Ordering.natural() as the comparator. This method provides more type-safety than builder(), as it can be called only for classes that implement Comparable.

        Note: the type parameter E extends Comparable<?> rather than Comparable<? super E> as a workaround for javac bug 6468354.

      • of

        @Deprecated
        public static <E> ImmutableSortedMultiset<E> of​(E e1)
        Deprecated.
        Pass a parameter of type Comparable to use of(Comparable).
        Not supported. You are attempting to create a multiset that may contain a non- Comparable element. Proper calls will resolve to the version in ImmutableSortedMultiset, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always
      • of

        @Deprecated
        public static <E> ImmutableSortedMultiset<E> of​(E e1,
                                                        E e2)
        Deprecated.
        Pass the parameters of type Comparable to use of(Comparable, Comparable).
        Not supported. You are attempting to create a multiset that may contain a non- Comparable element. Proper calls will resolve to the version in ImmutableSortedMultiset, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always
      • of

        @Deprecated
        public static <E> ImmutableSortedMultiset<E> of​(E e1,
                                                        E e2,
                                                        E e3)
        Deprecated.
        Pass the parameters of type Comparable to use of(Comparable, Comparable, Comparable).
        Not supported. You are attempting to create a multiset that may contain a non- Comparable element. Proper calls will resolve to the version in ImmutableSortedMultiset, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always
      • of

        @Deprecated
        public static <E> ImmutableSortedMultiset<E> of​(E e1,
                                                        E e2,
                                                        E e3,
                                                        E e4)
        Deprecated.
        Pass the parameters of type Comparable to use of(Comparable, Comparable, Comparable, Comparable).
        Not supported. You are attempting to create a multiset that may contain a non- Comparable element. Proper calls will resolve to the version in ImmutableSortedMultiset, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always
      • of

        @Deprecated
        public static <E> ImmutableSortedMultiset<E> of​(E e1,
                                                        E e2,
                                                        E e3,
                                                        E e4,
                                                        E e5)
        Deprecated.
        Pass the parameters of type Comparable to use of(Comparable, Comparable, Comparable, Comparable, Comparable) .
        Not supported. You are attempting to create a multiset that may contain a non- Comparable element. Proper calls will resolve to the version in ImmutableSortedMultiset, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always
      • copyOf

        @Deprecated
        public static <Z> ImmutableSortedMultiset<Z> copyOf​(Z[] elements)
        Deprecated.
        Pass parameters of type Comparable to use copyOf(Comparable[]).
        Not supported. You are attempting to create a multiset that may contain non- Comparable elements. Proper calls will resolve to the version in ImmutableSortedMultiset, not this dummy version.
        Throws:
        java.lang.UnsupportedOperationException - always